如何将这段XAML代码转换为C#中的代码隐藏代码,因为我的from和to值会动态更改?
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>
答案 0 :(得分:4)
您不需要代码隐藏的故事板,只能使用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)
试试这个:
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);
}
}