在Windows 8自动翻转图片 [英] Flip pictures automatically in windows 8

查看:138
本文介绍了在Windows 8自动翻转图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个翻转看法,我希望它能够自动改变图像的每个 N 不给用户选择点击左边或右边秒。


解决方案

使用的 DispatcherTimer 以控制在改变形象,并设置的SelectedIndex 上的FlipView (按名称 - 或者,如果你使用MVVM设置绑定到SelectedIndex的属性在视图模型)<。 / p>

修改



下面是一个使用文本块而不是图像(在内容的例子的项目 FlipView 对执行操作)没有影响。



MainPage.xaml中

 <第
X:类=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:可忽略= D字号=50粗细=大胆>

<网格背景={StaticResource的ApplicationPageBackgroundThemeBrush}>
< Grid.RowDefinitions>
< RowDefinition高度=自动/>
< RowDefinition />
< /Grid.RowDefinitions>
<! - 杖来展示一下我们不断变化的内容之间的间隔是一个地方 - >
< TextBlock的X:名称=TimeDelta文本=等待第一个变化.../>
将; FlipView X:名称=TheFlipView的SelectionChanged =DisplayedItemChangedGrid.Row =1>
<! - 静态添加一些项目 - >
< FlipView.Items>
< FlipViewItem>
< TextBlock的文本=项目1/>
< / FlipViewItem>
< FlipViewItem>
< TextBlock的文本=项目2/>
< / FlipViewItem>
< FlipViewItem>
< TextBlock的文本=项目3/>
< / FlipViewItem>
< /FlipView.Items>
< / FlipView>
< /网格和GT;
< /页>

MainPage.xaml.cs中



 使用系统; 
使用Windows.UI.Xaml;使用Windows.UI.Xaml.Controls
;
使用Windows.UI.Xaml.Navigation;

命名App1的
{
///<总结>
///可以在自己的一个框架内使用或导航到一个空白页。
///< /总结>
公共密封部分类的MainPage
{
//使存储定时器
私人只读DispatcherTimer _timer的地方;

//使保存最后一次显示的项目设置
私人的DateTime _lastChange的地方;

公众的MainPage()
{
的InitializeComponent();

//配置计时器
_timer =新DispatcherTimer
{
//设置刻度之间的间隔(在此情况下2秒看到它的工作)
间隔= TimeSpan.FromSeconds(2)
};

//改变时,计时器滴答
_timer.Tick + = ChangeImage什么显示;
//启动定时器
_timer.Start();
}

///<总结>
///变化的时候,计时器滴答
///<形象; /总结>
///< PARAM NAME =发件人>< /参数>
///< PARAM NAME =o的>< /参数>
私人无效ChangeImage(对象发件人,对象o)
{
//获取翻转视图
VAR的项目数TOTALITEMS = TheFlipView.Items.Count;
//找出新项目的索引(当前索引加一,如果下一个项目是超出范围,返回到零)
VAR newItemIndex =(TheFlipView.SelectedIndex + 1)%TOTALITEMS ;
//设置翻页查看
TheFlipView.SelectedIndex = newItemIndex显示项目的索引;
}

///<总结>
///调用时,这个网页是关于要显示在一个框架。
///< /总结>
///< PARAM NAME =E>事件数据介绍此页面是如何得出的。参数
///属性通常用于配置页和下; /参数>
保护覆盖无效的OnNavigatedTo(NavigationEventArgs E)
{
}

///<总结>
///当用户更改的项目手动显示,重置计时器,所以我们不会在最近的时间间隔
的剩余推进///< /总结>
///< PARAM NAME =发件人>< /参数>
///< PARAM NAME =E>< /参数>
私人无效DisplayedItemChanged(对象发件人,SelectionChangedEventArgs E)
{
//显示时间增量...
VAR currentTime的= DateTime.Now;

如果(_lastChange =默认值(DateTime的)!)
{
TimeDelta.Text =(currentTime的 - _lastChange)的ToString();
}

_lastChange = currentTime的;

//由于页配置之前,定时器,检查,以确保我们实际上已经得到了一个定时器
如果(!的ReferenceEquals(_timer,NULL))
{
_timer.Stop();
_timer.Start();
}
}
}
}


I have a flip view and I want it to change images automatically every n seconds without giving the user the option to click right or left.

解决方案

Use a DispatcherTimer to control the interval at which to change the image at and set the SelectedIndex property on the FlipView (by name - or setting the property bound to SelectedIndex on the viewmodel if you're using MVVM).

EDIT

Here's an example using text blocks instead of images (the content in the items of the FlipView have no bearing on performing the operation).

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();
            }
        }
    }
}

这篇关于在Windows 8自动翻转图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆