欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF实现定时刷新UI界面功能

程序员文章站 2023-10-30 14:39:28
本文实例为大家分享了wpf定时刷新ui界面展示的具体代码,供大家参考,具体内容如下 代码: using nhibernate.criterion; using...

本文实例为大家分享了wpf定时刷新ui界面展示的具体代码,供大家参考,具体内容如下

代码:

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的事件中执行,否则界面会卡,那么耗时的定时操作,比如查询数据库,需要再用一个 system.timers.timer,相对比较麻烦。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。