如何制作自动翻转视图 XAML

0 c# xaml windows-store-apps

我在 XAML 页面中创建了翻转视图,我想自动制作幻灯片交易,我该怎么做?

<StackPanel x:Name="StackPanel_1" Margin="541,42,71,160" Orientation="Vertical" Grid.Row="1">
        <FlipView x:Name="flipView1" Width="480" Height="270" 
          BorderBrush="Black" BorderThickness="1">
            <Grid Margin="0,0,-8,-8">
                <Image Source="Assets/Logo.png" Width="480" Height="270" Stretch="UniformToFill"/>
                <Border Background="#A5000000" Height="80" VerticalAlignment="Bottom">
                    <TextBlock Text="Logo" FontFamily="Segoe UI" FontSize="26.667" Foreground="#CCFFFFFF" Padding="15,20" Margin="0,0,8,8"/>
                </Border>
            </Grid>
            <Grid Margin="0,0,-8,-8">
                <Image Source="Assets/SplashScreen.png" Width="480" Height="270" Stretch="UniformToFill" />
                <Border Background="#A5000000" Height="80" VerticalAlignment="Bottom">
                    <TextBlock Text="Logo11111111" FontFamily="Segoe UI" FontSize="26.667" Foreground="#CCFFFFFF" Padding="15,20" Margin="0,0,8,8"/>
                </Border>
            </Grid>
            <Grid Height="270" Width="480">
                <Image Source="Assets/SmallLogo.png" Width="480" Height="270" Stretch="UniformToFill" />
                <Border Background="#A5000000" Height="80" VerticalAlignment="Bottom">
                    <TextBlock Text="Logo222222222" FontFamily="Segoe UI" FontSize="26.667" Foreground="#CCFFFFFF" Padding="15,20" Margin="0,0,8,8"/>
                </Border>
            </Grid>
        </FlipView>
Run Code Online (Sandbox Code Playgroud)

Rob*_*SFT 5

您需要更新翻转视图的 SelectedIndex 属性。

最直接的方法是运行 DispatcherTimer 并根据需要每增加一次 SelectedIndex 。当它到达末尾时,将其重新设置为 0。问题在于,当您将索引切换为 1 时,FlipView 会设置动画,但在您跳转页面时不会。如果您想从最后一页循环回第一页,它将跳转而不是动画。您可能想要反转方向而不是直接转到 0。

int change = 1;

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(2);
timer.Tick += (o, a) =>
    {
        // If we'd go out of bounds then reverse
        int newIndex = flipView1.SelectedIndex + change;
        if (newIndex >= flipView1.Items.Count || newIndex < 0)
        {
            change *= -1;
        }

        flipView1.SelectedIndex += change;
    };

timer.Start();
Run Code Online (Sandbox Code Playgroud)

如果你想在没有代码的情况下在 XAML 中完全设置它,那么你可以在 Xaml 中创建一个故事板动画来动画 SelectedIndex 并在页面加载时使用 EventTriggerBehavior 行为触发它。

<Page.Resources>
    <Storyboard x:Name="AutoFlipView" RepeatBehavior="Forever" AutoReverse="True">
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Selector.SelectedIndex)" Storyboard.TargetName="flipView1">
            <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                    <x:Int32>0</x:Int32>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
            <DiscreteObjectKeyFrame KeyTime="0:0:1">
                <DiscreteObjectKeyFrame.Value>
                    <x:Int32>1</x:Int32>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
            <DiscreteObjectKeyFrame KeyTime="0:0:2">
                <DiscreteObjectKeyFrame.Value>
                    <x:Int32>2</x:Int32>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
            <DiscreteObjectKeyFrame KeyTime="0:0:3">
                <DiscreteObjectKeyFrame.Value>
                    <x:Int32>2</x:Int32>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</Page.Resources>

<Interactivity:Interaction.Behaviors>
    <Core:EventTriggerBehavior EventName="Loaded">
        <Media:ControlStoryboardAction Storyboard="{StaticResource AutoFlipView}"/>
    </Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
Run Code Online (Sandbox Code Playgroud)