我有两个故事板动画。
xaml
<Window.Resources>
<Storyboard x:Key="hideMe">
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="0.0">
<DoubleAnimation.EasingFunction>
<ExponentialEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:0:0.1" Value="{x:Static Visibility.Hidden}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="showMe">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1.0">
<DoubleAnimation.EasingFunction>
<ExponentialEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</Window.Resources>
cs
public void ShowWindow()
{
Dispatcher.Invoke(new Action(delegate ()
{
(FindResource("showMe") as Storyboard).Begin(this);
}));
}
public void HideWindow()
{
Dispatcher.Invoke(new Action(delegate ()
{
(FindResource("hideMe") as Storyboard).Begin(this);
}));
}
如果我逐一运行两个故事板,它们会同时播放。
ShowWindow();
HideWindow();
我怎样才能等第一个动画结束后再运行第二个动画?
使用 Stortyboard
已完成 第一件事 Stortyboard
会运行的。
<Window.Resources>
<Storyboard x:Key="hideMe">
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="0.0">
<DoubleAnimation.EasingFunction>
<ExponentialEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:0:0.1" Value="{x:Static Visibility.Hidden}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="showMe">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1.0">
<DoubleAnimation.EasingFunction>
<ExponentialEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</Window.Resources>
后面的代码。
public void ShowWindow()
{
Dispatcher.Invoke(new Action(delegate ()
{
(FindResource("showMe") as Storyboard).Begin(this);
}));
}
private void Storyboard_Completed(object sender, EventArgs e)
{
Dispatcher.Invoke(new Action(delegate ()
{
(FindResource("hideMe") as Storyboard).Begin(this);
}));
}
注:在我的例子中 showMe
会先开始,完成后 hideMe
将开始。