在 WPF 图像框中每 10 秒更改一次图像 [英] Change image after each 10seconds in WPF image box

查看:25
本文介绍了在 WPF 图像框中每 10 秒更改一次图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF 的图片框有更新图片的需求.我正在考虑创建一个包含所有路径的列表,然后使用计时器控件检查 10 秒.经过 10 秒后,从列表中获取下一个 id 并绑定到图像框.我是 WPF 的新手.任何人都可以帮我举一个有效的例子.

There is a requirement of updating image in image boxes of WPF. I am thinking of creating a list with all the paths and then using a timer control checking the 10 seconds. After the 10 seconds has elapsed the next id from list is taken and bound to the image box. I am new to WPF. Can any one help me with a working example.

推荐答案

我已经用下面的代码运行了.

Hi i have made thig running with the below code .

private void timer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
    {

        Action action1 = () => this.BeginStoryboard((Storyboard)this.FindResource("BlinkStoryboardFed"));
        Dispatcher.BeginInvoke(action1);
        Action action = () => BindToImages(lststr);
        Dispatcher.BeginInvoke(action);
        //BindToImages(lststr);
        _timer.Start();
    }

public void BindToImages(List<string> lststrpath)
    {
        lock (_locker)
        {
            for (int i = 0; i < lststrpath.Count; i++)
            {
                if (count == 0)
                {
                    startindex = i;
                    this.BindToImgIndx = startindex;
                    AppState.Index = i;
                    BitmapImage img = new BitmapImage();
                    img.BeginInit();
                    img.UriSource = new Uri(lststrpath[startindex].ToString(), UriKind.Relative);
                    img.CacheOption = BitmapCacheOption.OnLoad;
                    img.EndInit();
                    image1.Source = img;
                    count++;
                }
                else
                {
                    int k = AppState.Index;
                    k = ++k;
                    this.BindToImgIndx = startindex;
                    if (k < lststrpath.Count)
                    {
                        BitmapImage img = new BitmapImage();
                        img.BeginInit();
                        img.UriSource = new Uri(lststrpath[k].ToString(), UriKind.Relative);
                        img.CacheOption = BitmapCacheOption.OnLoad;
                        img.EndInit();
                        image1.Source = img;
                    }
                    AppState.Index = k;
                }
                this.BeginStoryboard((Storyboard)this.FindResource("BlinkStoryboardUnFed"));
                break; 
            }
        }
    }

这篇关于在 WPF 图像框中每 10 秒更改一次图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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