In a WinUI 3 in Desktop app I have a property to update which is bound to the ui via x:Bind
.
I want to use the Dispatcher
like I do in WPF to get on the UI thread and avoid the thread error im getting when I update the prop:
System.Runtime.InteropServices.COMException: 'The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))'
Im just not sure how to do it in WinUI 3, when I try
DispatcherQueue.GetForCurrentThread().TryEnqueue(() =>
{
AeParty.OnSyncHub = false; // Prop bound in ui using x:Bind
});
I get this error
DispatcherQueue.GetForCurrentThread()
is null
I also tried:
this.DispatcherQueue.TryEnqueue(() =>
{
AeParty.OnSyncHub = false;
});
but it wont compile:
I then found this GitHub issue, so I tried:
SynchronizationContext.Current.Post((o) =>
{
AeParty.OnSyncHub = false;
}, null);
This works but why can't I get onto the UI thread with the Dispatcher in my VM?
string propertyName = null
instead, you can just useNotifyPropertyChanged()
w/o having to manually specify the property name. Also in the general case, it's better to test if _text is not equal to value before raising the event. – Balanchine