2014-03-25 74 views
3

如何將這段XAML代碼轉換爲C#中的代碼隱藏作爲我的動態和動態更改值?如何將XAML代碼的動畫轉換爲C#

XAML

<Window.Triggers> 
    <EventTrigger RoutedEvent="Window.Loaded"> 
     <BeginStoryboard> 
      <Storyboard> 
       <DoubleAnimation BeginTime="00:00:00"  
          From="200" 
          To="500" 
          Storyboard.TargetProperty="(Window.Top)"  
          AccelerationRatio=".1" 
          Duration="0:0:.2" /> 
      </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 
</Window.Triggers> 

回答

3

你不從共同需要的故事板這隻能在DoubleAnimation中完成。

public MainWindow() 
{ 
    InitializeComponent(); 
    Loaded += (s, e) => 
     { 
      DoubleAnimation animation = new DoubleAnimation(200, 500, 
              TimeSpan.FromSeconds(0.2)); 
      animation.AccelerationRatio = 0.1; 
      BeginAnimation(Window.TopProperty, animation); 
     }; 
} 
+1

+1:它比我的要短得多。 –

1

試試這個:

XAML

<Window x:Class="CreateAnimationinCodeHelp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Name="MyWindow" Loaded="Window_Loaded" 
     Title="MainWindow" Height="350" Width="525"> 

    <Grid> 
     <Label Background="AliceBlue" Content="Test" /> 
    </Grid> 
</Window> 

Code-behind

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     Storyboard sb = new Storyboard(); 

     DoubleAnimation doubleAnimation = new DoubleAnimation(); 
     doubleAnimation.From = 200; 
     doubleAnimation.To = 500; 
     doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.2)); 
     doubleAnimation.AccelerationRatio = 0.1; 

     Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Window.Top)")); 
     sb.Children.Add(doubleAnimation); 

     MyWindow.BeginStoryboard(sb); 
    } 
}