在GridView中翻转项目图像

时间:2012-11-21 06:49:13

标签: c# xaml windows-8 microsoft-metro

我是Windows 8 Modern UI Apps的新手,我想知道如何翻转平铺图像(位于GridView中的应用程序内部)。

我有一个以List形式从Web服务进入的图像路径,并希望使用该图像列表来翻转应用程序内的数据绑定项目背景。

我无法在网上找到任何东西所以我想我会在这里问。如果你知道从哪里开始,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用接受图片列表的用户控件。并在用户控件中使用DispatcherTimer每隔X秒切换/更改您的图片。

示例:

     public sealed partial class CustomFlip
    {
        #region Properties

        private readonly TimeSpan _delay = new TimeSpan(0, 0, 1);
        private readonly DispatcherTimer _dt;

        #endregion Properties

        #region Control Properties

        public IList<Uri> Pictures
        {
            get { return (IList<Uri>)GetValue(PicturesProperty); }
            set { SetValue(PicturesProperty, value); }
        }

        public static readonly DependencyProperty PicturesProperty
            = DependencyProperty.Register("Pictures", typeof(IList<Uri>), typeof(CustomFlip), null);

        #endregion Control Properties

        public CustomFlip()
        {
            InitializeComponent();
            Loaded += CLoaded;
            _dt = new DispatcherTimer { Interval = _delay };
            _dt.Tick += Refresh;

            Unloaded += CUnloaded;
        }

        private void CLoaded(object sender, RoutedEventArgs e)
        {
            _dt.Start();
        }

        private void CUnloaded(object sender, RoutedEventArgs e)
        {
            _dt.Stop();
        }

        private void Refresh(object sender, object e)
        {
            //Image name in your xaml.
            foo.Source = //Random Uri from Pictures.
//Sample this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;images/someimage.png", UriKind.Relative));
        }

        #region Methods

        public void StopRefresh()
        {
            _dt.Stop();
        }

        public void ReStartRefresh()
        {
            if (_dt == null)
                return;

            _dt.Start();
        }

        #endregion Methods
    }

你xaml:

<UserControl
    x:Name="root"
    x:Class="Foo"
    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"
    d:DesignHeight="300"
    d:DesignWidth="400">
        <Grid>
        <Image x:Name="Image"/>
        </Grid>

</UserControl>

问候。

编辑添加xaml示例:

xmlns:flip="using:Project.CustomControls.CustomFlip" <!--FlipLocation-->
<flip:ControlName Pictures={Binding YourList}/> <!--Hw to use it-->