WPF实现定时刷新UI界面功能
本文实例为大家分享了WPF定时刷新UI界面展示的具体代码,供大家参考,具体内容如下
代码:
usingNHibernate.Criterion;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Collections.ObjectModel;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Data;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Imaging;
usingSystem.Windows.Navigation;
usingSystem.Windows.Shapes;
usingVisifire.Charts;
namespaceSunCreate.CombatPlatform.Client
{
publicpartialclassMainPage:UserControl
{
privateSystem.Timers.TimertimerNotice=null;
publicMainPage()
{
InitializeComponent();
}
privatevoidMainPage_Loaded(objectsender,RoutedEventArgse)
{
#region通知公告
if(timerNotice==null)
{
BindNotice();
timerNotice=newSystem.Timers.Timer();
timerNotice.Elapsed+=newSystem.Timers.ElapsedEventHandler((o,eea)=>
{
BindNotice();
});
timerNotice.Interval=60*1000;
timerNotice.Start();
}
#endregion
}
privatevoidMainPage_SizeChanged(objectsender,SizeChangedEventArgse)
{
}
#region绑定通知公告
privatevoidBindNotice()
{
System.Threading.Tasks.Task.Factory.StartNew(()=>
{
try
{
inttotal=0;
TES_NOTICEinfo=newTES_NOTICE();
IListlist=newList();
list=HI.Get().GetListPage(null,DateTime.MinValue,DateTime.MinValue,1,50,reftotal);
Dispatcher.Invoke(newAction(()=>
{
noticeListView.ItemsSource=list;
}));
}
catch
{
}
});
}
#endregion
}
}
说明:在System.Timers.Timer的事件中使用BackgroundWorker是无效的,即如下代码不能正常刷新界面:
usingNHibernate.Criterion;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Collections.ObjectModel;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Data;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Imaging;
usingSystem.Windows.Navigation;
usingSystem.Windows.Shapes;
usingVisifire.Charts;
namespaceSunCreate.CombatPlatform.Client
{
publicpartialclassMainPage:UserControl
{
privateSystem.Timers.TimertimerNotice=null;
publicMainPage()
{
InitializeComponent();
}
privatevoidMainPage_Loaded(objectsender,RoutedEventArgse)
{
#region通知公告
if(timerNotice==null)
{
BindNotice();
timerNotice=newSystem.Timers.Timer();
timerNotice.Elapsed+=newSystem.Timers.ElapsedEventHandler((o,eea)=>
{
BindNotice();
});
timerNotice.Interval=60*1000;
timerNotice.Start();
}
#endregion
}
privatevoidMainPage_SizeChanged(objectsender,SizeChangedEventArgse)
{
}
#region绑定通知公告
privatevoidBindNotice()
{
PT_USER_INFOuser=newPT_USER_INFO();
IListtaskList=newList();
BackgroundWorkerworker=newBackgroundWorker();
worker.DoWork+=(s,e)=>
{
user=HI.Get().UserCache.GetCurrentUserInfo();
taskList=HI.Get().GetCombatTaskByUserIDUnfinished(user.ID.ToString());
};
worker.RunWorkerCompleted+=(s,e)=>
{
try
{
taskListView.ItemsSource=taskList;
}
catch{}
};
worker.RunWorkerAsync();
}
#endregion
}
}
也可以使用DispatcherTimer刷新界面,但耗时的操作不能放在DispatcherTimer的事件中执行,否则界面会卡,那么耗时的定时操作,比如查询数据库,需要再用一个System.Timers.Timer,相对比较麻烦。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。