WPF实现定时刷新UI界面的实例详解

这篇文章主要为大家详细介绍了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 list = new List();          list = HI.Get().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 taskList = new List();      BackgroundWorker worker = new BackgroundWorker();      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,相对比较麻烦。

以上就是WPF实现定时刷新UI界面的实例详解的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2439256.html

(0)
上一篇 2025年3月3日 12:00:48
下一篇 2025年2月25日 18:47:52

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • C#中关于TCP粘包出现的问题解决的示例

    这篇文章主要为大家详细介绍了c#中tcp粘包问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 一、TCP粘包产生的原理 1.TCP粘包是指发送方发送的若干包数据到接收方接收时粘成一包,从接收缓冲区看,后一包数据的头紧接着前一…

    2025年3月3日 编程技术
    200
  • HttpPostedFileBase文件上传实例详解

    这篇文章主要介绍了asp.net mvc httppostedfilebase文件上传,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文介绍了ASP.NET MVC HttpPostedFileBase文件上传 ,分享给大家,希望对大…

    编程技术 2025年3月3日
    200
  • 专业全能轻松 Carry 全场 华为 MateBook GT 14 凭实力迎接挑战

    9 月伊始,大学生们在无限的憧憬中迎来了新学期,9 月 4 日,华为携手专业创作博主、电竞选手走进高校,与师生们齐聚 ” 超能星光派对 ” 线下体验会,共同体验华为 matebook gt 14 在专业创作以及游戏场…

    2025年3月2日 互联网
    200
  • Go语言中的测试框架示范

    随着软件开发的高速发展,测试已经成为了保障软件质量的必不可少的环节,而测试框架是测试中的重要组成部分。本文将着重介绍go语言中常用的测试框架,并通过示范来加深对测试框架的理解。 Go语言中最流行的测试框架是testing包。在此之前我们需要…

    编程技术 2025年3月2日
    200
  • Go 语言中的文件操作的示例有哪些?

    go 语言作为一种新兴的编程语言,在文件操作方面虽然相对其他编程语言来说还算年轻,但已经有了十分强大和灵活的特性。本篇文章主要回答以下问题:go 语言中的文件操作的示例有哪些?首先,我们需要了解在 go 中,文件操作需要使用 os 包和 i…

    编程技术 2025年3月2日
    200
  • Golang Channels 的使用示例和案例分析

    Golang Channels 的使用示例和案例分析 引言:Golang是一种高效、并发性强的编程语言,它引入了一种名为”channel”的数据类型,用于实现不同的goroutine之间的通信。通过使用channel…

    2025年3月2日
    200
  • 深入解析Golang指针的用法,助你快速掌握

    Golang指针用法实例解析,让你快速上手 概述:在Go语言中,指针是一种特殊的变量类型,它存储了一个内存地址。通过指针,我们可以直接访问内存中的数据,能够在函数之间分享数据。指针功能强大且灵活,但也容易出错。本文将介绍Golang中指针的…

    2025年3月1日
    200
  • 学习Golang指针转换的最佳实践示例

    Golang 是一门功能强大且高效的编程语言,可以用于开发各种应用程序和服务。在 Golang 中,指针是一种非常重要的概念,它可以帮助我们更灵活和高效地操作数据。指针转换是指在不同类型之间进行指针操作的过程,本文将通过具体的实例来学习 G…

    2025年3月1日
    200
  • 用 Golang 编写计算方差的示例

    在Go语言中,计算一组数据的方差是一种常见的数学计算操作。方差用于衡量数据集中值的分散程度,是统计学中的重要概念之一。下面我们将通过一个示例来演示如何使用Go语言实现方差的计算。 首先,我们需要明确方差的定义:给定一组包含n个数据点的样本集…

    2025年3月1日
    200
  • Golang协程是什么?简单解析与实例说明

    Golang协程是什么?简单解析与实例说明 Golang是一门由Google开发的开源编程语言,其具有简洁、高效和并发性强的特点。在Golang中,协程(Goroutine)是其独有的并发处理机制,能够实现轻量级线程的并发操作,而不会占用过…

    2025年3月1日
    200

发表回复

登录后才能评论