使用异步方法在XAML中绑定系统时间

最近的工作需要在程序界面上显示实时的系统时间,网上查了查大部分都是用Timer或者线程来实现。

个人非常不喜欢用Timer,感觉这东西有点太耗资源,然后思考了下觉得更好的方法应该是使用异步的方法在委托中更新时间,比线程简单,效率和性能上又要比Timer好很多。

实现方法很简单啦,先定义一个DateTime属性,当然要实现INotifyPropertyChanged属性,以便前台绑定:

然后是异步方法,在适当的地方调用即可:

最后Xaml中绑定CurrentTime属性:

<TextBlock Text="{Binding CurrentTime,StringFormat={}{0:yyyy-MM-dd hh:mm:ss t\\M}}"/>

最终效果,还算可以:

原文地址:https://www.cnblogs.com/yanshouwang/p/8384030.html

时间: 2024-08-28 01:53:36

使用异步方法在XAML中绑定系统时间的相关文章

JAVA中获取系统时间

一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowString {public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式System.out.println(df.forma

C语言中的系统时间结构体类型

在C语言涉及中经常需要定时触发事件,涉及到获取系统时间,其结构体类型有多种.Unix/Linux系统下有以下几种时间结构: 1.time_t 类型:长整型,一般用来表示从1970-01-01 00:00:00时以来的秒数,精确度:秒:由函数time()获取: 该类型定义在头文件 /usr/include/sys/time.h 中: #define _TIME_T       typedef   long   time_t;             #endif 函数定义:time_t   tim

wpf中如何在xaml中绑定cs中类的属性

cs代码:/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ContentGrid.DataContext = this; this.Path = "数据绑定"; } public string Path { get; set;

xaml中绑定单例属性

在项目中经常会遇到,同一个字典表绑定到多个ItemsControl上的情况,可以在单例中创建一个List,xaml上绑定即可.看代码: 1,XAML 1 <Grid> 2 <StackPanel Orientation="Horizontal" 3 VerticalAlignment="Center"> 4 <TextBlock Text="分类" /> 5 <ComboBox ItemsSource=&

Talend 将Oracle中数据导入到hive中,根据系统时间设置hive分区字段

首先,概览下任务图: 流程是,先用tHDFSDelete将hdfs上的文件删除掉,然后将oracle中的机构表中的数据导入到HDFS中:建立hive连接->hive建表->tJava获取系统时间->tHiveLoad将hdfs上的文件导入到hive表中. 下面介绍每一个组件的设置: tHDFSDelete_1: 机构: tHDFSOutput_1: hive: tHiveCreateTable: tJava_1: tHiveLoad_1: 在Context下面建一个内容变量: 这个内容变

MFC在static text中动态显示系统时间

1.添加static text ,ID为IDC_ShowTime 2,在OnInitDialog()中 SetTimer(1,1000,NULL);         //启动定时器 3.添加WM_TIMER消息处理函数,ontimer中添加如下代码: CString strTime; CTime tm; tm=CTime::GetCurrentTime(); strTime=tm.Format("%Y-%m-%d %H:%M:%S"); SetDlgItemText(IDC_ShowT

C#中的系统时间获取问题

C#获取当前系统时间 2010-01-02 16:24 --DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 取当前年月日时分秒      currentTime=System.DateTime.Now; 取当前年     int 年=currentTime.Year; 取当前月     int 月=currentTime.Month; 取当前日     int 日=currentTime.Day; 取当前时    

VC++编程中获取系统时间

<span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDlg::OnBnClickedGettimeButton() { // TODO: 在此添加控件通知处理程序代码 //方法一 使用MFC的CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("

Linux驱动中获取系统时间

最近在做VoIP方面的驱动,总共有16个FXS口和FXO口依次初始化,耗用的时间较多.准备将其改为多线程,首先需要确定哪个环节消耗的时间多,这就需要获取系统时间. #include <linux/time.h> /*头文件*/ struct timeval time_now; unsigned long int time_num;//获取的时间 do_gettimeofday(&time_now); time_num = time_now.tv_sec*1000+time_now.tv