Application.OpenForms[0].在 WPF 中调用所需的等效项



我需要等效的Application.OpenForms[0]。InvokeRequired in WinForms for wpf.我试过

var dispatcher = myDispatcherObject.Dispatcher;
if (dispatcher.CheckAccess()) { /* ... */ }

但没有运气

尝试以下扩展方法:

public static void TryToExecuteOnUI(this Action uiAction)
{
var uiDispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
if (uiDispatcher.CheckAccess() == false)
{
uiDispatcher.Invoke(uiAction);
return;
}
uiAction();
}

最新更新