C# 如何制作自动翻转视图XAML

C# 如何制作自动翻转视图XAML,c#,xaml,windows-store-apps,C#,Xaml,Windows Store Apps,我在XAML页面中创建了flipview,我想让幻灯片自动转换,我该怎么做 <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=

我在XAML页面中创建了flipview,我想让幻灯片自动转换,我该怎么做

<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>

您需要更新flipview的SelectedIndex属性

最简单的方法是运行一个Dispatcher,并每隔多长时间递增SelectedIndex。当它到达终点时,将其设置回0。问题在于,当您将索引切换一次时,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();
int change=1;
调度程序计时器=新调度程序();
timer.Interval=TimeSpan.FromSeconds(2);
计时器。滴答+=(o,a)=>
{
//如果我们想越界,那就倒过来
int newIndex=flipView1.SelectedIndex+change;
如果(newIndex>=flipView1.Items.Count | | newIndex<0)
{
变化*=-1;
}
flipView1.SelectedIndex+=更改;
};
timer.Start();
如果您希望在XAML中完全设置此项,而不使用代码,则可以在XAML中创建故事板动画,以设置所选索引的动画,并在页面加载时使用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>

0
1.
2.
2.

您需要更新flipview的SelectedIndex属性

最简单的方法是运行一个Dispatcher,并每隔多长时间递增SelectedIndex。当它到达终点时,将其设置回0。问题在于,当您将索引切换一次时,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();
int change=1;
调度程序计时器=新调度程序();
timer.Interval=TimeSpan.FromSeconds(2);
计时器。滴答+=(o,a)=>
{
//如果我们想越界,那就倒过来
int newIndex=flipView1.SelectedIndex+change;
如果(newIndex>=flipView1.Items.Count | | newIndex<0)
{
变化*=-1;
}
flipView1.SelectedIndex+=更改;
};
timer.Start();
如果您希望在XAML中完全设置此项,而不使用代码,则可以在XAML中创建故事板动画,以设置所选索引的动画,并在页面加载时使用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>

0
1.
2.
2.

我写了一个哈克原型,展示了如何通过在
FlipView
中重新排列元素来设置整个循环的动画

C#

使用系统集合;
使用System.Collections.ObjectModel;
使用System.Linq;
使用System.Threading.Tasks;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
名称空间App4
{
公共类循环FlipView:FlipView
{
公共异步任务周期()
{
如果(this.ItemsSource!=null)
{
var list=(IList)this.ItemsSource;
如果(list.Count==0)
{
返回;
}
SelectionChangedEventHandler处理程序=null;
var tcs=new TaskCompletionSource();
处理程序=(s,e)=>
{
tcs.SetResult(真);
this.SelectionChanged-=处理程序;
};
this.SelectionChanged+=处理程序;
this.SelectedIndex=(this.SelectedIndex+1)%list.Count;
等待tcs任务;
等待任务。延迟(500);
var i=此.SelectedIndex;
this.SelectedItem=null;
变量项=列表[0];
移除列表(0);
列表。添加(项目);
这个。SelectedIndex=i-1;
}
else if(this.Items!=null)
{
if(this.Items.Count==0)
{
返回;
}
SelectionChangedEventHandler处理程序=null;
var tcs=new TaskCompletionSource();
处理程序=(s,e)=>
{
tcs.SetResult(真);
this.SelectionChanged-=处理程序;
};
this.SelectionChanged+=处理程序;
this.SelectedIndex=(this.SelectedIndex+1)%this.Items.Count;
等待tcs任务;
等待任务。延迟(500);
var i=此.SelectedIndex;
this.SelectedItem=null;
var item=this.Items[0];
此.Items.RemoveAt(0);
此.Items.Add(item);
这个。SelectedIndex=i-1;
}
}
公共异步任务自动循环()
{
while(true)
{
这个循环();
阿华