Windows应用商店UI更新

时间:2012-03-29 18:42:02

标签: c# xaml windows-runtime windows-store-apps

我正在为Windows 8编写Windows Store App玩具应用程序。 它只有一个带有TextBlock的xaml页面。该页面的MyTimer类为DataContext

this.DataContext = new MyTimer();

MyTimer实现INotifyPropertyChanged,并使用计时器更新属性Time

public MyTimer(){
    TimerElapsedHandler f = new TimerElapsedHandler(NotifyTimeChanged);
    TimeSpan period = new TimeSpan(0, 0, 1);
    ThreadPoolTimer.CreatePeriodicTimer(f, period);
}

private void NotifyTimeChanged(){
    if (this.PropertyChanged != null){
        this.PropertyChanged(this, new PropertyChangedEventArgs("Time"));
    }
}

TextBlock在时间上有数据绑定

<TextBlock Text="{Binding Time}" />

当我运行应用程序时,我有以下例外:

System.Runtime.InteropServices.COMException was unhandled by user code

带有消息

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

真正的问题是我正在更新MyTimer类的属性,而不是GUI本身, 我无法弄清楚,但我认为解决方案应该使用像this one这样的东西。

3 个答案:

答案 0 :(得分:7)

是的,您正在通过线程池线程而不是UI线程通知属性更改。您需要将通知编组回计时器回调中的UI线程。现在,您的视图模型与您的视图分离(一件好事)因此它没有到Dispatcher基础结构的直接链接。所以你想做的就是把它交给正确的SynchronizationContext进行交流。为此,您需要在构造期间捕获当前SynchronizationContext,或者允许它显式传递给适合测试的构造函数,或者如果您要从UI线程开始初始化对象。

整个shebang看起来像这样:

public class MyTimer
{
    private SynchronizationContext synchronizationContext;

    public MyTimer() : this(SynchronizationContext.Current)
    {
    }

    public MyTimer(SynchronizationContext synchronizationContext)
    {
        if(this.synchronizationContext == null)
        {
            throw new ArgumentNullException("No synchronization context was specified and no default synchronization context was found.")
        }

        TimerElapsedHandler f = new TimerElapsedHandler(NotifyTimeChanged);
        TimeSpan period = new TimeSpan(0, 0, 1);
        ThreadPoolTimer.CreatePeriodicTimer(f, period);
    }

    private void NotifyTimeChanged()
    {
        if(this.PropertyChanged != null)
        {
            this.synchronizationContext.Post(() =>
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Time"));
                });
        }
    }
}

答案 1 :(得分:5)

执行此操作的一种方法是在循环中等待Task.Delay()而不是使用计时器:

class MyTimer : INotifyPropertyChanged
{
    public MyTimer()
    {
        Start();
    }

    private async void Start()
    {
        while (true)
        {
            await Task.Delay(TimeSpan.FromSeconds(1));
            PropertyChanged(this, new PropertyChangedEventArgs("Time"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public DateTime Time { get { return DateTime.Now; } }
}

如果在UI线程上调用构造函数,它也会在那里调用PropertyChanged。好的是,完全相同的代码也适用于WPF(在.Net 4.5和C#5下)。

答案 2 :(得分:1)

此博客的代码如何:

http://metrowindows8.blogspot.in/2011/10/metro-tiles.html

这对我有用。 我必须将ThreadPoolTimer对象传递给我的委托函数

相关问题