来自 Style 的 Storyboard Completed 事件? [英] Storyboard Completed Event from Style?

查看:88
本文介绍了来自 Style 的 Storyboard Completed 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Storyboard 动画的新手,但我认为这可能是一个我无法通过简单方法解决的问题.尽管如此,我还是在这里碰碰运气,也许你们中的一些人知道如何帮助我.

I'm new to Storyboard animations but I think this might be a problem I can't workaround the easy way. Nevertheless I try my luck here, maybe some of you guys know how to help me.

我的场景:我想在我的应用程序中显示一个具有淡入淡出效果的弹出窗口.我还想通过 MVVM 执行此操作,因此我的包含淡入淡出效果和弹出窗口的控件不应使用代码隐藏,而我的应用程序只需要将此控件的数据上下文重置为新的视图模型即可显示新消息.

My Scenario : I want to show a popup in my Application that has a fadein effect. I also want to do this via MVVM so my control that wraps the fadein effect and the popup should no use codebehind and my application should just need to reset the datacontext of this control to a new viewmodel to show a new message.

我的问题是我无法确定动画何时完成,因为我需要在样式中设置淡入淡出动画.

My Problem is that I cannot determine when the animation is finished because I need to set the fadein Animation in the style.

我的 XAML 看起来像这样:

My XAML looks like this :

<UserControl.Resources>

    <Style x:Key="popupStyle" TargetType="{x:Type Border}" >
        <Style.Triggers>
            <Trigger Property="Visibility" Value="Visible">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard x:Name="FadingStoryBoard">
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.05" To="1" BeginTime="0:0:1"  Duration="0:0:2.5" >
                                <DoubleAnimation.EasingFunction>
                                    <ExponentialEase Exponent="5" EasingMode="EaseIn" />
                                </DoubleAnimation.EasingFunction>
                            </DoubleAnimation>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" BeginTime="0:0:6"  Duration="0:0:8.5" >
                                <DoubleAnimation.EasingFunction>
                                    <ExponentialEase Exponent="15" EasingMode="EaseOut" />
                                </DoubleAnimation.EasingFunction>
                            </DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

<Popup Name="Popup" IsOpen="{Binding IsVisible}" Height="{Binding PopupHeight}" Width="{Binding PopupWidth}" VerticalOffset="{Binding PopupVerticalOffset}" HorizontalOffset="{Binding PopupHorizontalOffset}" PopupAnimation="Fade" AllowsTransparency="True">
    <Border Style="{StaticResource popupStyle}" Name="PopupContent" Padding="1" BorderBrush="#000000" Background="AliceBlue" CornerRadius="5" BorderThickness="3,3,3,3">
        <!-- Events -->
        <interact:Interaction.Triggers>
            <interact:EventTrigger EventName="PreviewMouseDown">
                <cmd:EventToCommand Command="{Binding Path=PopupMouseDownCommand}" PassEventArgsToCommand="True" />
            </interact:EventTrigger>
        </interact:Interaction.Triggers>

        <DockPanel Name="ContentContainer" Background="Black" LastChildFill="True">
            <Image Source="{Binding MessageIcon}" DockPanel.Dock="Left" Margin="5,0,5,0" Width="32" Height="32" />
            <StackPanel Background="Transparent" DockPanel.Dock="Right" Margin="3">
                <TextBlock  Name="PopupHeaderTextBlock" Margin="0,3,0,5" TextWrapping="Wrap" FontSize="10" Text="{Binding PopupHeaderText}"  Foreground="White"  Background="Transparent" />
                <TextBlock Name="PopupTextBlock" Text="{Binding PopupText}" TextWrapping="Wrap" FontSize="10" Foreground="White" Background="Transparent" />
            </StackPanel>
        </DockPanel>
    </Border>
</Popup>

有没有人知道如何在 Storyboard 完成后在我的 ViewModel 中收到通知?

Anyone any ideas how I can get a notification in my ViewModel when the Storyboard has finished ?

推荐答案

您可以处理故事板上的 Completed 事件.文档在这里:http://msdn.microsoft.com/en-us/library/system.windows.media.animation.timeline.completed.aspx

You can handle the Completed event on the storyboard. Documentation Here: http://msdn.microsoft.com/en-us/library/system.windows.media.animation.timeline.completed.aspx

这是从代码隐藏附加事件的代码:从构造函数调用:

Here's the code to attach the event from the codebehind: call from the constructor:

private void AttachToCompletedEvent()
{
    Style popupStyle = Resources["popupStyle"];
    TriggerBase trigger = popupStyle.Triggers[0];
    BeginStoryboard action = trigger.EnterActions[0] as BeginStoryboard;
    Storyboard storyboard = action.Storyboard;
    storyboard.Completed += CompletedEventHandler;
}

我认为这应该适用于您提供的代码.

I think that should work for the code you provided.

这篇关于来自 Style 的 Storyboard Completed 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆