MAUI 相当于 Dispatcher.Run

问题描述 投票:0回答:1

在 WPF 应用程序中,我可以创建一个新线程,获取该线程的 Systems.Windows.Dispatcher,然后调用 Dispatcher.Run 获取新线程的调度程序服务。如果我在 MAUI 中新创建的线程上调用 Dispatcher.GetForCurrentThread(),它只会返回 null。 MAUI 中是否有类似的功能,可以在新创建的线程上进行类似调度程序的操作?

maui dispatcher
1个回答
0
投票

对于MAUI,有以下几种使用方式:

  • BindableObject.Dispatcher.Dispatch()
  • Dispatcher.GetForCurrentThread().Dispatch()
  • MainThread.BeginInvokeOnMainThread()

关于他们的具体区别,可以参考GitHub上mattleibow的回答,他说:

还有一个

Dispatcher.GetForCurrentThread()
可以用。这与 MainThread 不同,MainThread 将始终尝试查找 UI 线程。如果当前线程没有调度程序,
GetForCurrentThread
将返回 null。

另外,你还可以看到ToolmakerSteve的这个回答:For Dotnet Maui, What the Difference Between Application.Current?.Dispatcher.Dispatch(async()) and MainThread.InvokeOnMainThreadAsync.

希望这些可以帮助你:-)

© www.soinside.com 2019 - 2024. All rights reserved.