【发布时间】:2021-12-20 16:40:06
【问题描述】:
在桌面应用程序中的 WinUI 3 中,我有一个要更新的属性,该属性通过 x:Bind 绑定到 UI。
我想像在 WPF 中那样使用Dispatcher 来进入 UI 线程,并避免在更新道具时出现线程错误:
System.Runtime.InteropServices.COMException: 'The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))'
当我尝试时,我只是不确定如何在 WinUI 3 中做到这一点
DispatcherQueue.GetForCurrentThread().TryEnqueue(() =>
{
AeParty.OnSyncHub = false; // Prop bound in ui using x:Bind
});
我收到此错误
DispatcherQueue.GetForCurrentThread() 为空
我也试过了:
this.DispatcherQueue.TryEnqueue(() =>
{
AeParty.OnSyncHub = false;
});
但它不会编译:
然后我发现this GitHub问题,所以我尝试了:
SynchronizationContext.Current.Post((o) =>
{
AeParty.OnSyncHub = false;
}, null);
这可行,但为什么我不能在我的 VM 中使用 Dispatcher 进入 UI 线程?
【问题讨论】:
标签: c# xaml data-binding desktop winui-3