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

 更新时间:2017年07月03日 17:10:13   作者:秋荷雨翔  
这篇文章主要为大家详细介绍了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,相对比较麻烦。

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

相关文章

  • ASP.NET书籍信息录入实现代码

    ASP.NET书籍信息录入实现代码

    这篇文章主要介绍了ASP.NET书籍信息录入实现代码,特别适合网上书城项目中使用,需要的朋友可以参考下
    2015-10-10
  • .Net使用RabbitMQ即时发消息Demo

    .Net使用RabbitMQ即时发消息Demo

    RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统,下面这篇文章主要给大家介绍了关于.Net使用RabbitMQ即时发消息的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-07-07
  • asp.net使用母版页中使用ajax脚本取数据

    asp.net使用母版页中使用ajax脚本取数据

    因母版页继承自UserControl,我们无法像正常页面那样使用Jquey或Ajax的PageMethods等无刷新方法取数据。不过可以使用ajax提供的Sys.Net.WebRequest来解决这一问题。
    2010-09-09
  • .NET内存泄漏分析Windbg项目实例

    .NET内存泄漏分析Windbg项目实例

    这篇文章介绍了.NET内存泄漏分析Windbg项目实例,文中通过示例代码介绍的非常详细。对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • .NET中的状态机库Stateless的操作流程

    .NET中的状态机库Stateless的操作流程

    本文给大家介绍了.NET中的状态机库Stateless, 使用它我们可以很容易的定义出自己业务需要的状态机,或者基于状态机的工作流,本文大部分的内容都来自官方Github,有兴趣的同学可以深入研究一下
    2021-12-12
  • 增加asp.net应用程序性能的20种方法(简单有效)

    增加asp.net应用程序性能的20种方法(简单有效)

    增加asp.net应用程序性能的20种方法小结,需要的朋友可以参考下,对于服务器也需要一些设置。
    2010-01-01
  • ASP.NET中后台注册js脚本使用的方法对比

    ASP.NET中后台注册js脚本使用的方法对比

    接下来为大家介绍下使用Page.ClientScript.RegisterClientScriptBlock 和Page.ClientScript.RegisterStartupScript:区别
    2013-04-04
  • WPF中自定义GridLengthAnimation

    WPF中自定义GridLengthAnimation

    这篇文章主要为大家详细介绍了WPF中自定义GridLengthAnimation的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • asp.net 动态输出透明gif图片

    asp.net 动态输出透明gif图片

    要使用asp.net动态输出透明gif图片,也就是用Response.ContentType = "image/GIF"。
    2009-12-12
  • .NET Core 反射底层原理解析

    .NET Core 反射底层原理解析

    本文介绍了.NET中的反射机制,包括前期绑定(EarlyBinding)和后期绑定(LateBinding)的概念,反射是一种强大的工具,但在性能要求较高的场景中应谨慎使用,感兴趣的朋友跟随小编一起看看吧
    2024-11-11

最新评论