【问题标题】:DispatcherQueue null when trying to update Ui property in ViewModel尝试更新 ViewModel 中的 Ui 属性时 DispatcherQueue null
【发布时间】: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


    【解决方案1】:

    DispatcherQueue.GetForCurrentThread() 仅在实际具有DispatcherQueue 的线程上调用时返回DispatcherQueue。如果您在后台线程上调用它,则确实不会返回 DispatcherQueue

    所以诀窍是在 UI 线程上调用该方法并将返回值存储在一个变量中,然后您可以从后台线程中使用该变量,例如:

    public sealed partial class MainWindow : YourBaseClass
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }
    
        public ViewModel ViewModel { get; } = new ViewModel();
    }
    
    public class ViewModel : INotifyPropertyChanged
    {
        private readonly DispatcherQueue _dispatcherQueue = DispatcherQueue.GetForCurrentThread();
    
        public ViewModel()
        {
            Task.Run(() => 
            {
                for (int i = 0; i < 10; i++)
                {
                    string val = i.ToString();
                    _dispatcherQueue.TryEnqueue(() =>
                    {
                        Text = val;
                    });
                    Thread.Sleep(2000);
                }
            });
    
        }
        private string _text;
        public string Text
        {
            get { return _text; }
            set { _text = value; NotifyPropertyChanged(nameof(Text)); }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多