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

网络编程 发布日期:2024/10/15 浏览次数:1

正在浏览:WPF实现定时刷新UI界面功能

本文实例为大家分享了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,相对比较麻烦。

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

一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?