两个故事板同步

Two storyboard synchronization

我有 2 个故事板动画。

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);
    }));
}

如果我运行一个接一个2个故事板他们会同时播放

ShowWindow();
HideWindow();

我如何等待第一个动画结束后 运行 第二个动画?

使用第一个 StoryboardCompleted 事件 运行:

<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 将开始。