2012-02-27 26 views
0

我正在使用一個調度計時器設置爲每20秒觸發一次,其中我需要通過它在我的視圖模型中進行更新數據綁定文本塊。這是在視圖模型計時器代碼:用戶界面沒有得到更新在Windows Phone 7從計時器方法

public TestViewModel() 
    { 
    _dispatcherTimer = new DispatcherTimer(); 
    _dispatcherTimer.Interval = new TimeSpan(0, 0, 20); 
    _dispatcherTimer.Tick += new EventHandler(_dispatcherTimer_Tick); 
    _dispatcherTimer.Start();  
    } 

void _dispatcherTimer_Tick(object sender, EventArgs e) 
    { 
     if(blah == blah) 
     { 
     _dispatcher.BeginInvoke(() => 
         { 
          // DoneEnabled is a boolean property that my 
          // textblock's IsEnabled property is bound to 
          DoneEnabled = true; 
         }); 
     } 
    } 

private bool _doneEnabled;  

    /// <summary> 
    /// Gets or sets a value indicating whether [done enabled]. 
    /// </summary> 
    /// <value> 
    /// <c>true</c> if [done enabled]; otherwise, <c>false</c>. 
    /// </value> 
    public bool DoneEnabled 
    { 
     get { return _doneEnabled; } 
     set 
     { 
      _doneEnabled = value; 
      RaisePropertyChanged("DoneEnabled"); 
     } 
    } 

按鈕被啓用的其他地方,當我試圖通過更新此屬性除了在計時器的事件處理程序在我的視圖模型這麼做。任何想法,如果我在這裏失去一些東西?

謝謝。

UPDATE:

編輯

void _dispatcherTimer_Tick(object sender, EventArgs e) 
    { 
     if(blah == blah) 
     {   
      // DoneEnabled is a boolean property that my 
      // textblock's IsEnabled property is bound to 
      DoneEnabled = true; 

     } 
    } 

的_dispatcherTimer_Tick方法如下

就像一個魅力!

+0

只是想掩蓋顯而易見的問題:您是否驗證了您的viewmodel已正確連接到您的視圖?你確認你確實設置了文本塊的綁定嗎? – earthling 2012-02-28 00:58:18

回答

1

由於您使用的是DispatchTimer方法_dispatcherTimer_Tick已經的UI /發送器線程上運行(這是建立在使用的方便性) - 只是做:

void _dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    if(blah == blah) 
    { 
     DoneEnabled = true; 
    } 
} 

MSDN

使用與System.Timers.Timer 相反的DispatcherTimer的原因是DispatcherTimer在與分派器 相同的線程上運行, DispatcherPriority可以在DispatcherTimer上設置。

+0

這是正確的。另外請記住BeginInvoke是異步並將改變,但只要線程'感覺像' – MyKuLLSKI 2012-02-27 22:33:45

+0

感謝您的回覆!我刪除了_dispatcher.BeginInvoke()並更新了上面的代碼。但它仍然沒有顯示。 – Cranialsurge 2012-02-28 00:39:29

+0

好的,所以與我試圖做某事的時間有關的問題。由於操作是異步的。它拋棄了我的假設。 – Cranialsurge 2012-02-28 02:35:20

相關問題