WPF實現(xiàn)定時刷新UI界面功能
本文實例為大家分享了WPF定時刷新UI界面展示的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
using NHibernate.Criterion; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Linq; using System.Text; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Visifire.Charts; namespace SunCreate.CombatPlatform.Client { public partial class MainPage : UserControl { private System.Timers.Timer timerNotice = null; public MainPage() { InitializeComponent(); } private void MainPage_Loaded(object sender, RoutedEventArgs e) { #region 通知公告 if (timerNotice == null) { BindNotice(); timerNotice = new System.Timers.Timer(); timerNotice.Elapsed += new System.Timers.ElapsedEventHandler((o, eea) => { BindNotice(); }); timerNotice.Interval = 60 * 1000; timerNotice.Start(); } #endregion } private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e) { } #region 綁定通知公告 private void BindNotice() { System.Threading.Tasks.Task.Factory.StartNew(() => { try { int total = 0; TES_NOTICE info = new TES_NOTICE(); IList<TES_NOTICE> list = new List<TES_NOTICE>(); list = HI.Get<INoticeService>().GetListPage(null, DateTime.MinValue, DateTime.MinValue, 1, 50, ref total); Dispatcher.Invoke(new Action(() => { noticeListView.ItemsSource = list; })); } catch { } }); } #endregion } }
說明:在 System.Timers.Timer 的事件中使用 BackgroundWorker 是無效的,即如下代碼不能正常刷新界面:
using NHibernate.Criterion; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Linq; using System.Text; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Visifire.Charts; namespace SunCreate.CombatPlatform.Client { public partial class MainPage : UserControl { private System.Timers.Timer timerNotice = null; public MainPage() { InitializeComponent(); } private void MainPage_Loaded(object sender, RoutedEventArgs e) { #region 通知公告 if (timerNotice == null) { BindNotice(); timerNotice = new System.Timers.Timer(); timerNotice.Elapsed += new System.Timers.ElapsedEventHandler((o, eea) => { BindNotice(); }); timerNotice.Interval = 60 * 1000; timerNotice.Start(); } #endregion } private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e) { } #region 綁定通知公告 private void BindNotice() { PT_USER_INFO user = new PT_USER_INFO(); IList<TES_COMBAT_TASK> taskList = new List<TES_COMBAT_TASK>(); BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (s, e) => { user = HI.Get<Cache.ICacheService>().UserCache.GetCurrentUserInfo(); taskList = HI.Get<ITaskService>().GetCombatTaskByUserIDUnfinished(user.ID.ToString()); }; worker.RunWorkerCompleted += (s, e) => { try { taskListView.ItemsSource = taskList; } catch { } }; worker.RunWorkerAsync(); } #endregion } }
也可以使用 DispatcherTimer 刷新界面,但耗時的操作不能放在DispatcherTimer的事件中執(zhí)行,否則界面會卡,那么耗時的定時操作,比如查詢數(shù)據(jù)庫,需要再用一個 System.Timers.Timer,相對比較麻煩。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
.Net獲取URL中文參數(shù)值的亂碼問題解決方法總結(jié)
這篇文章主要介紹了.Net獲取URL中文參數(shù)值的亂碼問題解決方法,總結(jié)分析了針對URL參數(shù)傳遞中出現(xiàn)的亂碼問題與相應(yīng)的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下2016-08-08ASP.NET對HTML頁面元素進行權(quán)限控制(二)
分三步實現(xiàn):標出界面所要分配權(quán)限的元素、掃描界面獲取所要分配權(quán)限的元素信息、存入數(shù)據(jù)庫中,感興趣的朋友可以了解下2013-12-12.Net Core在程序的任意位置使用和注入服務(wù)的方法
這篇文章主要介紹了.Net Core在程序的任意位置使用和注入服務(wù)的方法,很多朋友對此問題都不是很清楚,于是寫這篇文章幫助大家學(xué)習(xí),需要的朋友可以參考下2018-10-10.NET使用報表工具FastReport實現(xiàn)打印功能
這篇文章介紹了.NET使用報表工具FastReport實現(xiàn)打印功能的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03解決uploadify使用時session發(fā)生丟失問題的方法
這篇文章主要為大家詳細介紹了uploadify使用時發(fā)現(xiàn)session發(fā)生丟失問題的解決方法,遇到過類似問題的朋友可以參考本文進行解決2016-05-05ASP.NET(C#)中操作SQLite數(shù)據(jù)庫實例
最近項目中有使用到SQLite數(shù)據(jù)庫,于是查找資料,編寫了一個ASP.NET基于C#語言的SQLite數(shù)據(jù)庫操作實例.大家看代碼就可以看懂了,和以往使用ADO.NET操作SQL數(shù)據(jù)庫類似.2009-12-12asp.net DataTable相關(guān)操作集錦(篩選,取前N條數(shù)據(jù),去重復(fù)行,獲取指定列數(shù)據(jù)等)
這篇文章主要介紹了asp.net DataTable相關(guān)操作,包括篩選,取前N條數(shù)據(jù),去重復(fù)行,獲取指定列數(shù)據(jù)等.基本涵蓋了DataTable的常見操作技巧,需要的朋友可以參考下2016-06-06