6

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

等待 RefreshUserInfoAsync();

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

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

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

强调文本

4

1 回答 1

18

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.

Here is the update:

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.

Mike

于 2013-02-19T17:50:27.057 回答