C# Windows Phone 7中未从计时器方法更新UI

C# Windows Phone 7中未从计时器方法更新UI,c#,windows-phone-7,xaml,C#,Windows Phone 7,Xaml,我使用的调度计时器设置为每20秒启动一次,在此期间,我需要通过视图模型中的属性更新数据绑定文本块。这是viewmodel中的计时器代码: public TestViewModel() { _dispatcherTimer = new DispatcherTimer(); _dispatcherTimer.Interval = new TimeSpan(0, 0, 20); _dispatcherTimer.Tick += new EventHandler(_di

我使用的调度计时器设置为每20秒启动一次,在此期间,我需要通过视图模型中的属性更新数据绑定文本块。这是viewmodel中的计时器代码:

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");
        }
    }

工作起来很有魅力

由于您使用的是
DispatchTimer
方法
\u DispatchTimer\u Tick
已经在UI/Dispatcher线程上运行(这是为方便使用而内置的)-只需执行以下操作:

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

使用与System.Timers.Timer相反的Dispatcher的原因 Dispatcher是否与Dispatcher在同一线程上运行 并且可以在DispatcherPriority上设置DispatcherPriority


这是正确的。还要记住,BeginInvoke是异步的,并且会更改,但是只要线程“感觉像它”,谢谢您的回复!我删除了_dispatcher.BeginInvoke(),并更新了上面的代码。但是它仍然没有显示出来。好吧,所以时间的问题,我试图做一些事情。因为操作是异步的。它抛弃了我的假设。只是想掩盖一个显而易见的事实:您是否验证了viewmodel与视图的连接是否正确?您是否已验证确实设置了textblock的绑定?
void _dispatcherTimer_Tick(object sender, EventArgs e)
{
    if(blah == blah)
    {
        DoneEnabled = true;  
    }
}