winrt-xaml - Windows 8 App 应用程序调用了为不同线程编码的接口(interface)

标签 winrt-xaml

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

await RefreshUserInfoAsync();

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

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 交谈过他给了我答案。从 View Model 中,我可以通过 CoreApplication.MainView.CoreWindow 访问 Dispatcher。

这是更新:

var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;

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

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

麦克风

关于winrt-xaml - Windows 8 App 应用程序调用了为不同线程编码的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14805543/

相关文章:

xaml - 在 Windows 8.1 中创建主题

c# - VisualStateManager GoToState 不起作用

windows - Windows 8 Store App 中的 XAML 对齐

windows-8 - 如何使用在任何分辨率下都看起来不错的字体设计 Metro UI?

xaml - Metro 风格 : Scrolling with mouse wheel

c# - Windows 运行时 : How to set image in a ellipse shape button?

c# - Windows Phone 8.1 应用程序中的本地设置未存储

c# - 本地文件夹在 Windows 8 中消失

c# - 从 Windows 8 应用程序中生成的缓冲区播放声音

windows-8 - Windows 8 软键盘未隐藏