本篇文章為大家展示了利用WPF怎么實(shí)現(xiàn)一個(gè)定時(shí)刷新UI界面功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

代碼:
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
 }
}上述內(nèi)容就是利用WPF怎么實(shí)現(xiàn)一個(gè)定時(shí)刷新UI界面功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
                本文名稱:利用WPF怎么實(shí)現(xiàn)一個(gè)定時(shí)刷新UI界面功能-創(chuàng)新互聯(lián)
                
                文章URL:http://www.chinadenli.net/article0/igsio.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、服務(wù)器托管、ChatGPT、用戶體驗(yàn)、微信公眾號、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
