2012-05-17 41 views
0

我有一個主頁,它根據當前時間從本地xml文件中獲取信息。隨着時間流逝刷新頁面上的信息?

當應用程序啓動時,它會檢查當前時間並基於它獲取正確的xml值並將其顯示在文本塊中。但它沒有更新。我正在考慮在頁面上放置一個刷新按鈕,但更聰明的人會建議DispatchTimer或Timer。我應該使用什麼,以及我應該在哪裏放置代碼?我是非常基本的用戶,所以請如果你可以具體和給出代碼示例,這將是高興的讚賞。

謝謝大家:)我的一小部分代碼。

var obj = filteredData.First(); 

    TimeSpan currentTime = myDay.TimeOfDay; 
    string result = String.Empty; 
    string Prayer = String.Empty; 

    if (currentTime >= obj.Fajr && currentTime < obj.Sunrise) 
    { 
     result = "Fajr"; 
     Prayer = obj.Fajr.ToString(@"hh\:mm"); 

    } 
    else if (currentTime >= obj.Sunrise && currentTime < obj.Zohr) 
    { 
     result = "Sunrise"; 
     Prayer = obj.Sunrise.ToString(@"hh\:mm"); 

    } 

    textBlock3.Text = result; 
    textBlock4.Text = Prayer; 

回答

0

這可能與視圖模型和後臺線程可以更好地接近,所以東西這樣的:

public class PrayerViewModel : DependencyObject 
    public PrayerViewModel() 
    { 
     // TODO: Start off a new thread that Raises the PropertyChanged() Event for each property at the right time 
    } 

    public string Result 
    { 
     get 
     { 
      TimeSpan currentTime = myDay.TimeOfDay; 
      if (currentTime >= obj.Fajr && currentTime < obj.Sunrise) 
      { 
       return "Fajr"; 
      } 
      else if (currentTime >= obj.Sunrise && currentTime < obj.Zohr) 
      { 
       return "Sunrise"; 
      } 
     } 
    } 
    public string Prayer 
    { 
     get 
     { 
      TimeSpan currentTime = myDay.TimeOfDay; 
      if (currentTime >= obj.Fajr && currentTime < obj.Sunrise) 
      { 
       Prayer = obj.Fajr.ToString(@"hh\:mm"); 
      } 
      else if (currentTime >= obj.Sunrise && currentTime < obj.Zohr) 
      { 
       Prayer = obj.Sunrise.ToString(@"hh\:mm"); 
      } 
     } 
    } 
}