SelectionChanged 事件的 FlipView EventTrigger

FlipView EventTrigger for the SelectionChanged event

我正在开发通用应用程序。在一个页面上,我决定使用 FlipView。我可以轻松地从代码隐藏中为 SelectionChanged 事件制作动画,但我只是好奇是否有办法仅使用 XAML 为该事件制作动画。 (顺便说一句,UseTouchAnimationsForAllNavigation="True" 不起作用)。 所以,这是我正在做的简化示例:

    <FlipView x:Name="MultipleItems">
              <FlipView.Triggers>
                <EventTrigger RoutedEvent="Selector.SelectionChanged">
                    <BeginStoryboard>
                        <Storyboard x:Name="ColorStoryboard">
                            //do stuff
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
              <FlipView.Triggers>
   </FlipView>

我认为这种使用 EventTrigger 的方式很好(就 SelectionChanged 事件采用从 RoutedEventArgs 继承的参数而言),但在导航到包含 FlipView 的页面时它仍然给我运行时错误。

下一个错误:

    WinRT information: Failed to assign to property 'Windows.UI.Xaml.EventTrigger.RoutedEvent'. [Line: 69 Position: 35]

   Additional information: The text associated with this error code could not be found.

我相信有办法正确分配 RoutedEvent 属性,但我还没有找到它。我也不会为这么简单的事情使用行为。

有人可以帮忙吗?

您需要在项目中安装 Microsoft.Xaml.Behaviors.Uwp.Managed。那么 EventTrigger 将在 UWP 项目中得到支持。

然后在您的 XAML 中像这样使用这个包:

xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
xmlns:Media="using:Microsoft.Xaml.Interactions.Media"

现在您可以像这样更改 FlipView 的背景颜色:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.Resources>
        <Storyboard x:Key="std" x:Name="std" >
            <ColorAnimation From="Red" To="Transparent" Duration="0:0:3" 
                            Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                            Storyboard.TargetName="flipView"/>
        </Storyboard>
    </Grid.Resources>
    <FlipView x:Name="flipView" ItemsSource="{x:Bind flipviewCollection}">
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="SelectionChanged">
                <Media:ControlStoryboardAction Storyboard="{StaticResource std}" />
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
        <FlipView.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding ImageSource}" Stretch="None"/>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>
</Grid>

如您所见,我使用了 EventTriggerBehavior,事件的名称是 SelectionChanged