c# - Windows 8自动翻转图片

标签 c# image

我有一个翻转 View ,我希望它每 n 秒自动更改图像,而不让用户选择向右或向左单击。

最佳答案

使用 DispatcherTimer控制更改图像的时间间隔并设置 SelectedIndex FlipView上的属性(property)(按名称 - 如果您使用的是 MVVM,则在 View 模型上设置绑定(bind)到 SelectedIndex 的属性)。

编辑

这是一个使用文本 block 而不是图像的示例(FlipView 的项目中的内容与执行操作无关)。

MainPage.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" FontSize="50" FontWeight="Bold">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <!-- Stick in a place to show what our interval between changing content is -->
        <TextBlock x:Name="TimeDelta" Text="Waiting for first change..."/>
        <FlipView x:Name="TheFlipView" SelectionChanged="DisplayedItemChanged" Grid.Row="1">
            <!-- Statically add some items -->
            <FlipView.Items>
                <FlipViewItem>
                    <TextBlock Text="Item1" />
                </FlipViewItem>
                <FlipViewItem>
                    <TextBlock Text="Item2" />
                </FlipViewItem>
                <FlipViewItem>
                    <TextBlock Text="Item3" />
                </FlipViewItem>
            </FlipView.Items>
        </FlipView>
    </Grid>
</Page>

MainPage.xaml.cs

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage
    {
        //Make a place to store the timer
        private readonly DispatcherTimer _timer;

        //Make a place to store the last time the displayed item was set
        private DateTime _lastChange;

        public MainPage()
        {
            InitializeComponent();

            //Configure the timer
            _timer = new DispatcherTimer
            {
                //Set the interval between ticks (in this case 2 seconds to see it working)
                Interval = TimeSpan.FromSeconds(2)
            };

            //Change what's displayed when the timer ticks
            _timer.Tick += ChangeImage;
            //Start the timer
            _timer.Start();
        }

        /// <summary>
        /// Changes the image when the timer ticks
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="o"></param>
        private void ChangeImage(object sender, object o)
        {
            //Get the number of items in the flip view
            var totalItems = TheFlipView.Items.Count;
            //Figure out the new item's index (the current index plus one, if the next item would be out of range, go back to zero)
            var newItemIndex = (TheFlipView.SelectedIndex + 1) % totalItems;
            //Set the displayed item's index on the flip view
            TheFlipView.SelectedIndex = newItemIndex;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        /// <summary>
        /// When the user changes the item displayed manually, reset the timer so we don't advance at the remainder of the last interval
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DisplayedItemChanged(object sender, SelectionChangedEventArgs e)
        {
            //Show the time deltas...
            var currentTime = DateTime.Now;

            if (_lastChange != default(DateTime))
            {
                TimeDelta.Text = (currentTime - _lastChange).ToString();
            }

            _lastChange = currentTime;

            //Since the page is configured before the timer is, check to make sure that we've actually got a timer
            if (!ReferenceEquals(_timer, null))
            {
                _timer.Stop();
                _timer.Start();
            }
        }
    }
}

关于c# - Windows 8自动翻转图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14177037/

相关文章:

c# - 为什么不能在 C# 中声明静态结构,但它们可以有静态方法?

c# - ASP.NET MVC 中的 DRY - 详细信息显示与编辑表单

c# - 启动应用程序时 advapi32.dll 中的 EntryPointNotFoundException

android - 如何优化分辨率为 1920x1080 的图像,以使其作为背景?

image - Silverlight 应用程序图像在运行时不显示

c# - 如何在 WP7 上的 SQL 数据库中存储图像

android - 网络图像在 android Flutter 中不显示

c# - 字典值返回时删除了斜杠 '\'?

javascript - 从图像映射启动新窗口,设置其大小和参数

c# - 将 C# 7.2 'in' 参数修饰符放在各处是否有用