我想使用mvvm模式在wpf屏幕中持續更新屏幕上顯示當前時間。使用MVVM在WPF中連續顯示當前時間顯示
我在我的視圖模型
編寫這些代碼和我寫了這個方法
public string GetCurrentDateTime()
{
try
{
DispatcherTimer timer = new DispatcherTimer(new TimeSpan(0, 0, 1),
DispatcherPriority.Normal,
delegate
{
this.CurrentDateTime = DateTime.Now.ToString("HH:mm:ss");
},
this.Dispatcher);
return CurrentDateTime;
}
catch
{
return CurrentDateTime;
}
}
我綁定我的文本塊與屬性,但它正顯示出異常,因爲this.CurrentDateTime爲空。
有什麼建議爲什麼?
保重!當GetCurrentDateTime被調用時,你總是啓動一個新的定時器。 – PVitt