有没有办法在View中触发事件的故事板?

时间:2017-04-22 07:26:29

标签: c# uwp

我有一个按钮:

    <Button x:Name="button" Click="{x:Bind ViewModel.OnButtonClick}">
        <Button.Projection>
            <PlaneProjection RotationZ="50"/>
        </Button.Projection>
        <Button.Triggers>
            <EventTrigger>
                <BeginStoryboard>
                    <Storyboard x:Name="Storyboard1">
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationZ)" Storyboard.TargetName="button">
                            <EasingDoubleKeyFrame KeyTime="0" Value="50"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="320"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>

我使用MVVM模式,因此在单击按钮后,将调用该方法。我也想开始一个故事板,但我发现只有MVVM模式的解决方案,因为我无法在我的ViewModel中引用Storyboard(所以我无法在那里启动它)。

有没有办法修改此故事板只能在按钮点击时启动?现在它在应用程序启动时启动。

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到 - 在资源中定义 Storyboard ,然后从代码开始,在按钮的 Style <中使用 VisualStates / em>甚至使用行为扩展程序在xaml中执行所有操作。后者可能如下所示(首先添加对行为XAML 的引用):

<!--add needed namespaces-->
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:icore="using:Microsoft.Xaml.Interactions.Core"
xmlns:imedia="using:Microsoft.Xaml.Interactions.Media"

<Button x:Name="button" >
    <Button.Projection>
        <PlaneProjection RotationZ="50"/>
    </Button.Projection>
    <interactivity:Interaction.Behaviors>
        <icore:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=button}">
            <imedia:ControlStoryboardAction>
                <imedia:ControlStoryboardAction.Storyboard>
                    <Storyboard x:Name="Storyboard1">
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationZ)" Storyboard.TargetName="button">
                            <EasingDoubleKeyFrame KeyTime="0" Value="50"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="320"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </imedia:ControlStoryboardAction.Storyboard>
            </imedia:ControlStoryboardAction>
        </icore:EventTriggerBehavior>
    </interactivity:Interaction.Behaviors>
</Button>