WPF實(shí)現(xiàn)定時(shí)刷新UI界面功能
本文實(shí)例為大家分享了WPF定時(shí)刷新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 刷新界面,但耗時(shí)的操作不能放在DispatcherTimer的事件中執(zhí)行,否則界面會(huì)卡,那么耗時(shí)的定時(shí)操作,比如查詢數(shù)據(jù)庫,需要再用一個(gè) System.Timers.Timer,相對比較麻煩。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
.Net使用RabbitMQ即時(shí)發(fā)消息Demo
RabbitMQ是一個(gè)在AMQP基礎(chǔ)上完整的,可復(fù)用的企業(yè)消息系統(tǒng),下面這篇文章主要給大家介紹了關(guān)于.Net使用RabbitMQ即時(shí)發(fā)消息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
asp.net使用母版頁中使用ajax腳本取數(shù)據(jù)
因母版頁繼承自UserControl,我們無法像正常頁面那樣使用Jquey或Ajax的PageMethods等無刷新方法取數(shù)據(jù)。不過可以使用ajax提供的Sys.Net.WebRequest來解決這一問題。2010-09-09
.NET內(nèi)存泄漏分析Windbg項(xiàng)目實(shí)例
這篇文章介紹了.NET內(nèi)存泄漏分析Windbg項(xiàng)目實(shí)例,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
.NET中的狀態(tài)機(jī)庫Stateless的操作流程
本文給大家介紹了.NET中的狀態(tài)機(jī)庫Stateless, 使用它我們可以很容易的定義出自己業(yè)務(wù)需要的狀態(tài)機(jī),或者基于狀態(tài)機(jī)的工作流,本文大部分的內(nèi)容都來自官方Github,有興趣的同學(xué)可以深入研究一下2021-12-12
增加asp.net應(yīng)用程序性能的20種方法(簡單有效)
增加asp.net應(yīng)用程序性能的20種方法小結(jié),需要的朋友可以參考下,對于服務(wù)器也需要一些設(shè)置。2010-01-01
asp.net 動(dòng)態(tài)輸出透明gif圖片
要使用asp.net動(dòng)態(tài)輸出透明gif圖片,也就是用Response.ContentType = "image/GIF"。2009-12-12

