Windows Store App UI update

孤者浪人 提交于 2019-12-10 03:59:59

问题


I am writing a Windows Store App toy application for Windows 8. It has just one xaml page with a TextBlock. The page has the class MyTimer as DataContext :

this.DataContext = new MyTimer();

MyTimer implements INotifyPropertyChanged and the updating of the property Time is made with a timer:

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

with

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

the TextBlock has a databinding on Time

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

When I run the application i have the following exception:

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

With the message

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

The real problem is that I am updating the property of the class MyTimer, not the GUI itself, I can't figure it out, but I think the solution should use something like this one.


回答1:


Yes, you're notifying property changes from a thread pool thread rather than the UI thread. You need to marshal the notification back to the UI thread in the timer callback. Now, your view model is separated from your view (a good thing) therefore it doesn't have a direct link to the Dispatcher infrastructure. So what you want to do is hand it the proper SynchronizationContext on which to communicate. To do this you need to capture the current SynchronizationContext during construction or allow it to be passed in explicitly to a constructor which is good for tests or if you're initializing the object off the UI thread to begin with.

The whole shebang would look something like this:

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



回答2:


One way to do this is awaiting Task.Delay() in a loop instead of using a timer:

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

If you call the constructor on the UI thread, it will invoke the PropertyChanged there too. And the nice thing is that exactly the same code will work for example in WPF too (under .Net 4.5 and C# 5).




回答3:


how about the code from this blog:

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

This worked for me. I had to pass a ThreadPoolTimer object to my delegate function



来源:https://stackoverflow.com/questions/9931453/windows-store-app-ui-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!