【问题标题】:Flipping Item Images inside GridView在 GridView 中翻转项目图像
【发布时间】:2012-11-09 09:12:11
【问题描述】:

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

我有来自 Web 服务的列表形式的图像路径,并希望使用该图像列表来翻转应用内的数据绑定项背景。

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

    您可以使用接受图片列表的用户控件。并在您的用户控件中使用 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-->
    

    【讨论】:

    • 嗨大卫,感谢您的及时回复。但是,上面的代码不起作用并且缺少一些参数。例如EpgProgressBar 的返回类型以及其他问题。即使我修复了它,我也无法弄清楚如何正确使用它。你有工作样本吗?
    • @user1027620 您好,这是一个工作示例。我只是错过了通过 CustomFlip 更改 EpgProgressBar (它不是方法,而是构造函数)。它是一个用户控件,因此您需要从您的页面中调用它并为其提供您的列表(例如使用其他控件)。
    • 再次感谢,还有一个问题是这一行get { return (Uri)GetValue(PicturesProperty); } 它需要一个列表IList&lt;System.Uri&gt; 我应该如何更改它?
    • 是的,再次抱歉。我忘记了。
    • 您能否分享一个关于如何在 XAML 页面中使用上述用户控件的示例?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2016-01-22
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多