Windows 8 App 应用程序调用了一个为不同线程编组的接口 [英] Windows 8 App The application called an interface that was marshalled for a different thread

查看:16
本文介绍了Windows 8 App 应用程序调用了一个为不同线程编组的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 c#/XAML 开发 Windows 8 应用程序.除了这个事件处理程序,我在这一行收到以下错误之外,一切都在工作.

I am working on a Windows 8 application using c#/XAML. Everything has been working except for this event handler where I get the following error on this line.

await RefreshUserInfoAsync();

应用程序调用了一个为不同线程编组的接口.(来自 HRESULT 的异常:0x8001010E (RPC_E_WRONG_THREAD))

此 Observable 集合正在从应用程序类中的推送通知处理程序更新,并且此事件处理程序在我的视图模型中.我没有使用任何框架,如 MVVM Light.我查看了其他一些关于此的帖子并尝试创建我自己的 Dispatcher Helper,但我收到了一个不同的错误,其中 Window.Current.Dispatcher 为空.任何想法如何使这项工作?

This Observable Collection is being updated from a push notification handler in the app class and this event handler is in my view model. I'm not using any frameworks like MVVM Light. I looked at some of the other posts on this and tried creating my own Dispatcher Helper but I received a different error where Window.Current.Dispatcher is null. Any ideas how to make this work?

    private async void PushActions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        string action = e.NewItems[0] as string;
        if(action != null)
        {
             await RefreshUserInfoAsync();
        }
    }


    private async Task RefreshUserInfoAsync()
    {
        var userInfos = await SessionRepository.GetSessionUsersWithInfoAsync(SessionGuid, RoundGuid);
        this.UserInfoList = new ObservableCollection<UserInfo>(userInfos);
    }

强调文字

推荐答案

我和 Jeffrey Richter 他给了我答案.从视图模型,我可以通过 CoreApplication.MainView.CoreWindow 到达 Dispatcher.

I talked with Jeffrey Richter and he gave me the answer. From the View Model, I can get to the Dispatcher via CoreApplication.MainView.CoreWindow.

这里是更新:

var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;

await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
    this.UserInfoList = new ObservableCollection<UserInfo>(userInfos);
});

我希望这对其他人有帮助.

I hope this helps someone else.

迈克

这篇关于Windows 8 App 应用程序调用了一个为不同线程编组的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆