Dispatcher
当您没有引用任何 UI 元素时,有什么方法可以获取 UI 线程?
问问题
38235 次
2 回答
104
您可以从静态应用程序实例中获取 UI Dispatcher:Application.Current.Dispatcher
您可能需要先检查Application.Current
null,因为它可以在关机序列期间被清除。
于 2010-04-19T21:54:36.567 回答
7
在碰巧也在使用 WPF 的 WinForms 应用程序中使用以下对我来说效果更好(在我的情况下是 Esri 的 Arcmap.exe)
private System.Windows.Threading.Dispatcher Dispatcher { get; set; }
// I put this in my view model constructor as it MUST be called from UI thread
Dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
每次我打开一个新的 WPF 窗口然后关闭它时,另一种方法(涉及调用new System.Windows.Application()
populate )都会给我带来问题。System.Windows.Application.Current
这不仅无效System.Windows.Application.Current
,而且我不能再打开新窗口作为他们的InitializeComponents()
方法,都失败了:
System.InvalidOperationException:“应用程序对象正在关闭。”
到目前为止,新的解决方案没有这些副作用。
于 2018-12-10T22:06:53.807 回答