none
WPF的Image控件,动态加载图片显示时为什么在一个时间间隔后会出现隔一个图片然后显示了下一张呢?调试显示被忽略的那张图片的源已经改变,却不显示 RRS feed

  • 问题

  • private DispatcherTimer gCycleTimer = new DispatcherTimer();//创建定时器
            private delegate void TimerDispatcherDelegate();
            private int gTimerCount = 0;    //
    用于循环播放时的计数操作
            private bool playIndex;

            /// <summary>
            ///
    函数名称:play_auto_Click
            ///
    函数功能:鼠标点击"自动播放"按钮后,image_Center动态加载选择了特定参数和日期的数据图像
            /// </summary>
            private void play_auto_Click(object sender, RoutedEventArgs e)
            {
                GetAllImage();

                gCycleTimer.Tick += new EventHandler(gCycleTimer_Tick);//事件处理程序gCycleTimer_Tick被添加到 dispatcherTimer类的Tick事件中
                gCycleTimer.Interval = new TimeSpan(0, 0, 2);//
    使用TimeSpan对象将Interval设置为2

                if (gDisplayCount > 0)
                {
                    gTimerCount = 0;
                    gCycleTimer.Start();
                }
                else
                {
                    MessageBox.Show("
    没有符合要求的图片资源", "系统消息", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }

            /// <summary>
            ///
    函数名称:gCycleTimer_Tick
            /// </summary>
            private void gCycleTimer_Tick(object sender, EventArgs e)
            {
                this.Dispatcher.Invoke(DispatcherPriority.Normal, new TimerDispatcherDelegate(UpdateImage));
            }

            /// </summary>
            ///
    函数名称:play_auto_Click
            /// </summary>
            private void play_stop_Click(object sender, RoutedEventArgs e)
            {
                gCycleTimer.Stop();
            }

            /// </summary>
            ///
    函数名称:UpdateImage
            /// </summary>
            private void UpdateImage()
            {
                try
                {
                    //
    加载日期标签
                    if (gDataCycleIndex == str_daily)
                    {
                        str_date = dateTransfer(gCyclePlayImagePath[gTimerCount].Substring(23, 7));
                    }
                    else
                    {
                        str_date = dateTransfer(gCyclePlayImagePath[gTimerCount].Substring(23, 7)) + "-" + dateTransfer(gCyclePlayImagePath[gTimerCount].Substring(30, 7));
    ​                }
                    label2.Content = str_date;

                    Console.WriteLine(gCyclePlayImagePath[gTimerCount]);

                    BitmapImage gDataImage = new BitmapImage();
                    gDataImage.BeginInit();
                    gDataImage.UriSource = new Uri(gCyclePlayImagePath[gTimerCount++], UriKind.Absolute);
                    gDataImage.EndInit();
                    image_Center.Source = gDataImage;
                    gTimerCount = gTimerCount % gDisplayCount;
                }
                catch (System.Exception ex)
                {
                    gCycleTimer.Stop();
                    MessageBox.Show(ex.Message);
                }

            }

            /// <summary>
            ///
    函数名称:GetAllImage
            ///
    函数功能:获取要循环播放的一组图片的路径集合
            /// </summary>
            private string[] gCyclePlayImagePath = new string[50];
            private int gDisplayCount = 0;

            private void GetAllImage()
            {
                string[] ImageFileName = Directory.GetFiles(gImagePath); //
    获取当前目录下所有PNG图像文件的名称
                gDisplayCount = 0;
                string findname = gDataCycleIndex + gParIndex;
                //Console.WriteLine("findname:{0}", findname);
                foreach (string file in ImageFileName)
                {
                    if (file.IndexOf(findname) != -1)
                    {
                        gCyclePlayImagePath[gDisplayCount] = file;
                        //Console.WriteLine("gCyclePlayImagePath[gDisplayCount++]:{0}", gCyclePlayImagePath[gDisplayCount]);
                        gDisplayCount++;                  
                    }
                }
                //MessageBox.Show(gDisplayCount.ToString());
            }

           测试时,第一次auto_play后是正常显示图片,然后点停止播放,第二次在点auto_play后就开始隔一张显示一张,这是为什么?求解答。。。

    2014年6月22日 4:24

答案

  • 你好,

    我在本地创建了一个项目来还原这个问题,简化了一些代码。请注意加黑的代码:

            private DispatcherTimer gCycleTimer = new DispatcherTimer();//创建定时器
            
            private delegate void TimerDispatcherDelegate();
            private int gTimerCount = 0;    //用于循环播放时的计数操作
            private bool playIndex;
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void StartButton_Click(object sender, RoutedEventArgs e)
            {
                GetAllImage();
    
                gCycleTimer.Tick += new EventHandler(gCycleTimer_Tick);//事件处理程序gCycleTimer_Tick被添加到 dispatcherTimer类的Tick事件中
                gCycleTimer.Interval = new TimeSpan(0, 0, 2);//使用TimeSpan对象将Interval设置为2秒
                gCycleTimer.IsEnabled = true;
                if (ImageFileName.Length > 0)
                {
                    gTimerCount = 0;
                    gCycleTimer.Start();
                }
                else
                {
                    MessageBox.Show("没有符合要求的图片资源", "系统消息", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
    
            private void gCycleTimer_Tick(object sender, EventArgs e)
            {
                this.Dispatcher.Invoke(DispatcherPriority.Normal, new TimerDispatcherDelegate(UpdateImage));
            }
    
            private void StopButon_Click(object sender, RoutedEventArgs e)
            {
                gCycleTimer.Tick -= new EventHandler(gCycleTimer_Tick);
                gCycleTimer.IsEnabled = false;
                gCycleTimer.Stop();
                //gCycleTimer.Interval = new TimeSpan(0, 0, 2);
                gTimerCount = 0;
                ImageFileName = null;
            }
    
            /// <summary>
            ///函数名称:GetAllImage
            ///函数功能:获取要循环播放的一组图片的路径集合
            /// </summary>
            string[] ImageFileName = null;
            private void GetAllImage()
            {
               ImageFileName = Directory.GetFiles(@"D:\Project\Resources\Images"); //获取当前目录下所有PNG图像文件的名称
            }
    
            /// </summary>
            /// 函数名称:UpdateImage
            /// </summary>
            private void UpdateImage()
            {
                try
                {
                    if (gTimerCount > ImageFileName.Length -1)
                    {
                        gTimerCount = 0;
                    }
                    else
                    { 
                        BitmapImage gDataImage = new BitmapImage();
                        gDataImage.BeginInit();
                        gDataImage.UriSource = new Uri(ImageFileName[gTimerCount], UriKind.Absolute);
                        gDataImage.EndInit();
                        myImage.Source = gDataImage;
                        txtFileName.Text = ImageFileName[gTimerCount].ToString();
                        gTimerCount++;
                    }
                }
                catch (System.Exception ex)
                {
                    gTimerCount = 0;
                    gCycleTimer.Stop();
                    MessageBox.Show(ex.Message);
                }
            }

    在Stop事件注销掉。否则会调用两次。

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月23日 6:55
    版主

全部回复

  • 你好,

    我在本地创建了一个项目来还原这个问题,简化了一些代码。请注意加黑的代码:

            private DispatcherTimer gCycleTimer = new DispatcherTimer();//创建定时器
            
            private delegate void TimerDispatcherDelegate();
            private int gTimerCount = 0;    //用于循环播放时的计数操作
            private bool playIndex;
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void StartButton_Click(object sender, RoutedEventArgs e)
            {
                GetAllImage();
    
                gCycleTimer.Tick += new EventHandler(gCycleTimer_Tick);//事件处理程序gCycleTimer_Tick被添加到 dispatcherTimer类的Tick事件中
                gCycleTimer.Interval = new TimeSpan(0, 0, 2);//使用TimeSpan对象将Interval设置为2秒
                gCycleTimer.IsEnabled = true;
                if (ImageFileName.Length > 0)
                {
                    gTimerCount = 0;
                    gCycleTimer.Start();
                }
                else
                {
                    MessageBox.Show("没有符合要求的图片资源", "系统消息", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
    
            private void gCycleTimer_Tick(object sender, EventArgs e)
            {
                this.Dispatcher.Invoke(DispatcherPriority.Normal, new TimerDispatcherDelegate(UpdateImage));
            }
    
            private void StopButon_Click(object sender, RoutedEventArgs e)
            {
                gCycleTimer.Tick -= new EventHandler(gCycleTimer_Tick);
                gCycleTimer.IsEnabled = false;
                gCycleTimer.Stop();
                //gCycleTimer.Interval = new TimeSpan(0, 0, 2);
                gTimerCount = 0;
                ImageFileName = null;
            }
    
            /// <summary>
            ///函数名称:GetAllImage
            ///函数功能:获取要循环播放的一组图片的路径集合
            /// </summary>
            string[] ImageFileName = null;
            private void GetAllImage()
            {
               ImageFileName = Directory.GetFiles(@"D:\Project\Resources\Images"); //获取当前目录下所有PNG图像文件的名称
            }
    
            /// </summary>
            /// 函数名称:UpdateImage
            /// </summary>
            private void UpdateImage()
            {
                try
                {
                    if (gTimerCount > ImageFileName.Length -1)
                    {
                        gTimerCount = 0;
                    }
                    else
                    { 
                        BitmapImage gDataImage = new BitmapImage();
                        gDataImage.BeginInit();
                        gDataImage.UriSource = new Uri(ImageFileName[gTimerCount], UriKind.Absolute);
                        gDataImage.EndInit();
                        myImage.Source = gDataImage;
                        txtFileName.Text = ImageFileName[gTimerCount].ToString();
                        gTimerCount++;
                    }
                }
                catch (System.Exception ex)
                {
                    gTimerCount = 0;
                    gCycleTimer.Stop();
                    MessageBox.Show(ex.Message);
                }
            }

    在Stop事件注销掉。否则会调用两次。

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月23日 6:55
    版主
  • 哦,好的,多谢~

    2014年6月24日 0:48