2014-02-19 42 views
0

所以我试图创建一个基本的动画,当闹钟响时触发。动画将是一个不透明的动画,将整个UI或其部分的不透明度降低到0或50(取决于部分)。动画和路由事件处理程序

现在尝试了很多关于降低对象不透明度的教程之后,它命中所有这些动画教程都运行了“Button.Click”或“Button.IsEnabled”事件。我需要使用任何类型的按钮或某个其他用户界面单击来启动我的应用程序。

我没有发现这一点,就说明做出RoutedEvent MSDN Create Custom RoutedEvent

代码中,我需要增加它的能力如下:

 private void timer_Tick(object sender, EventArgs e) 
    { 
     TimeTop.Content = DateTime.Now.ToString("h" + ":" + "mm" + " " + "tt"); 
    } 

    private void dispatcherTimer1_Tick(object sender, EventArgs e) 
    { 
     label1.Content = DateTime.Now.ToString("hh") + ":" + DateTime.Now.ToString("mm") + " " + DateTime.Now.ToString("t"+2); 
    } 

    private void dispatcherTimer2_Tick(object sender, EventArgs e) 
    { 
     //Alarm settings 
     if (label1.Content.Equals(label2.Content)) 
     { 
      //Actual Wake Up Call 
      //TOOK OUT WAKE UP CALL 
      TimeOfDayCB.Text = ""; 
      HourAlarmCB.Text = ""; 
      MinuteAlarmCB.Text = ""; 
      label2.Content = ""; 
     } 
    } 

那么我将如何去有关添加呢?也是我给的有用的来源,可以帮助我完成这项任务?

回答

0

基本上,我发现了一种方法来让动画在C#编码本身发生,没有事件触发器或XAML或故事板。 要重新创建这是你需要的全部内容:MSDN Animation Without Storyboard

1

要做到你想要的最好的方法是使用DataTrigger,绑定到代码中的某个属性。这种动画最容易从代码启动。

这里是启动动画时绑定属性变化的风格:

<Style x:Key="timerTriggeredFlash" TargetType="Label"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, 
        Path=DataContext.StartFlashing}" Value="True"> 
      <DataTrigger.EnterActions> 
       <BeginStoryboard> 
        <Storyboard> 
         <DoubleAnimation Storyboard.TargetProperty="Opacity" 
              From="0" To="1" RepeatBehavior="3x" /> 
        </Storyboard> 
       </BeginStoryboard> 
      </DataTrigger.EnterActions> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

请注意,我的ViewModel有一个公共布尔属性,StartFlashing,当我把它设置为true,使用该样式的标签上有它的不透明度动画开启和关闭三次。

使用的样式,请参阅样式名称,当你宣布你的标签,就像这样:

<Label Grid.Row="1" 
     Background="Red" 
     Style="{StaticResource timerTriggeredFlash}" 
     Content="Flashing label" /> 

我的视图模型是这样的:

public class MainWindowViewModel : INotifyPropertyChanged { 
    DispatcherTimer _Timer; 

    public event PropertyChangedEventHandler PropertyChanged; 

    public MainWindowViewModel() { 
     _Timer = new DispatcherTimer(); 
     _Timer.Interval = TimeSpan.FromSeconds(1); 
     _Timer.Tick += new EventHandler(_Timer_Tick); 
     _SecondsLeft = 3; 
     _Timer.Start(); 
    } 

    void _Timer_Tick(object sender, EventArgs e) { 
     SecondsLeft = SecondsLeft - 1; 
     if (_SecondsLeft <= 0) { 
      StartFlashing = true; 
      _Timer.Stop(); 
     } 
    } 

    private int _SecondsLeft; 
    public int SecondsLeft { 
     get { return _SecondsLeft; } 
     set { 
      _SecondsLeft = value; 
      if (PropertyChanged != null) { 
       PropertyChanged(this, new PropertyChangedEventArgs("SecondsLeft")); 
      } 
     } 
    } 

    private bool _StartFlashing; 
    public bool StartFlashing { 
     get { return _StartFlashing; } 
     set { 
      _StartFlashing = value; 
      if (PropertyChanged != null) { 
       PropertyChanged(this, new PropertyChangedEventArgs("StartFlashing")); 
      } 
     } 
    } 
} 

最后,我的主窗体的代码 - 后面连接到我的ViewModel是这样的:

public partial class MainWindow : Window { 
    public MainWindow() { 
     InitializeComponent(); 
     this.DataContext = new MainWindowViewModel(); 
    } 
} 

这里的关键是

  1. DataTriggers允许你从代码启动动画
  2. 你DataTrigger应绑定到一个属性在您的DataContext,在这种情况下,一个ViewModel
  3. 如果你已经用的ViewModels之前没有奏效,你可以绑定其公共属性,如果您实现INotifyPropertyChanged或创建您的属性作为DependencyProperties