WPF如何实现定时刷新UI界面

  介绍

这篇文章将为大家详细讲解有关WPF如何实现定时刷新UI界面,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

代码:

;
  使用系统;
  使用System.Collections.Generic;
  使用System.Collections.ObjectModel;
  使用System.ComponentModel;
  使用System.Data;
  使用来;
  使用text;
  使用System.Threading;
  使用System.Windows;
  使用System.Windows.Controls;
  使用System.Windows.Data;
  使用System.Windows.Documents;
  使用System.Windows.Input;
  使用System.Windows.Media;
  使用System.Windows.Media.Imaging;
  使用System.Windows.Navigation;
  使用System.Windows.Shapes;
  使用Visifire.Charts;
  
  名称空间SunCreate.CombatPlatform.Client
  {
  公共部分班级主页:用户控件
  {
  私人System.Timers。计时器timerNotice=零;
  
  公共主页()
  {
  InitializeComponent ();
  }
  
  私人空间MainPage_Loaded(对象发送方,RoutedEventArgs e)
  {
  #地区通知公告
  如果(timerNotice==null)
  {
  BindNotice ();
  
  timerNotice=new与system . timers . timer类();
  timerNotice。运行新System.Timers +=lapsedEventHandler ((o, eea)=比;
  {
  BindNotice ();
  });
  timerNotice。间隔=60 * 1000;
  timerNotice.Start ();
  }
  # endregion
  }
  
  私人空间MainPage_SizeChanged(对象发送方,SizeChangedEventArgs e)
  {
  
  }
  
  #地区绑定通知公告
  私人空间BindNotice ()
  {
  System.Threading.Tasks.Task.Factory.StartNew(()=比;
  {
  试一试
  {
  int总=0;
  TES_NOTICE信息=new TES_NOTICE ();
  IList列表=new List ();
  
  列表=HI.Get ()。GetListPage (null, DateTime。MinValue DateTime。MinValue 1 50,裁判总);
  
  调度员。调用(新操作(()=比;
  {
  noticeListView。ItemsSource=列表;
  }));
  }
  抓
  {
  
  }
  });
  }
  # endregion
  
  }
  }

说明:在System.Timers。计时器的事件中使用BackgroundWorker是无效的,即如下代码不能正常刷新界面:

;
  使用系统;
  使用System.Collections.Generic;
  使用System.Collections.ObjectModel;
  使用System.ComponentModel;
  使用System.Data;
  使用来;
  使用text;
  使用System.Threading;
  使用System.Windows;
  使用System.Windows.Controls;
  使用System.Windows.Data;
  使用System.Windows.Documents;
  使用System.Windows.Input;
  使用System.Windows.Media;
  使用System.Windows.Media.Imaging;
  使用System.Windows.Navigation;
  使用System.Windows.Shapes;
  使用Visifire.Charts;
  
  名称空间SunCreate.CombatPlatform.Client
  {
  公共部分班级主页:用户控件
  {
  私人System.Timers。计时器timerNotice=零;
  
  公共主页()
  {
  InitializeComponent ();
  }
  
  私人空间MainPage_Loaded(对象发送方,RoutedEventArgs e)
  {
  #地区通知公告
  如果(timerNotice==null)
  {
  BindNotice ();
  
  timerNotice=new与system . timers . timer类();
  timerNotice。运行新System.Timers +=lapsedEventHandler ((o, eea)=比;
  {
  BindNotice ();
  });
  timerNotice。间隔=60 * 1000;
  timerNotice.Start ();
  }
  # endregion
  }
  
  私人空间MainPage_SizeChanged(对象发送方,SizeChangedEventArgs e)
  {
  
  }
  
  #地区绑定通知公告
  私人空间BindNotice ()
  {
  PT_USER_INFO用户=new PT_USER_INFO ();
  IListtaskList=new List ();
  
  BackgroundWorker工人=new BackgroundWorker ();
  工人。DoWork +=(年代,e)=比;
  {
  .UserCache.GetCurrentUserInfo用户=HI.Get () ();
  taskList=HI.Get () .GetCombatTaskByUserIDUnfinished (user.ID.ToString ());
  
  };
  工人。RunWorkerCompleted +=(年代,e)=比;
  {
  试一试
  {
  taskListView。ItemsSource=taskList;
  }
  抓住{}
  };
  worker.RunWorkerAsync ();
  }
  # endregion
  
  }
  }

也可以使用DispatcherTimer刷新界面,但耗时的操作不能放在DispatcherTimer的事件中执行,否则界面会卡,那么耗时的定时操作,比如查询数据库,需要再用一个System.Timers。计时器,相对比较麻烦。

WPF如何实现定时刷新UI界面