gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:14:02 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com