Windows – Application.Current在ElementHost为null

我在我的个人Libs中使用WPF UserControl。 Libs包含在我的WPF和WindowsForms程序中。现在我的UserControl必须显示一个新的(WPF)窗口。在新的窗口我想设置所有者。我这样做:
dialog.Owner = Application.Current.MainWindow;

如果我在WPF程序中使用UserControl,这可以正常工作。

当我在WindowsForms程序中使用UserControl(我将ElementHost elementHost.Child = …中的UserControl设置为Application.Current null)。

这不是很好,我的程序抛出异常。

为什么是Application.Current null?

应用程序。特定于WPF应用程序。
因此,当您在WinForms应用程序中使用WPF控件时,您需要初始化WPF应用程序的实例。在您的WinForms应用程序中执行此操作。
if ( null == System.Windows.Application.Current )
{
   new System.Windows.Application();
}

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...