I am working on a project with WinUI 3 with C++/winrt. I need to update my UI in real-time with many data from a thread. The thread is working for receiving the data from the server. And my UI , working as a client, wants to display the data as the thread receives in real-time. I looked on the Internet and some suggested to use "Dispatcher.Invoke" to do so, like the following code:
private void Button_Click(object sender, RoutedEventArgs e)
{
Task.Run(() => Compute());
}
private void Compute()
{
// perform computation here
Dispatcher.Invoke(CoreDispatcherPriority.Normal, ShowText, this, resultString);
}
private void ShowText(object sender, InvokedHandlerArgs e)
{
this.TextBlock.Text = (string)e.Context;
}
( URL for the code )
But it is for WPF project, I wonder how shall I do it in WinUI 3 with C++ under the namespace winrt? Or any other workaround?
I don't know if I understand it right. I am really new to it.
And because WinUI 3 is released only last year, I couldn't find any other solution for my application.
Any suggestions will be helpful to me. And an example code would be perfect!
Thanks you in advance.