加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

Windows – Application.Current在ElementHost为null

发布时间:2020-12-13 20:49:54 所属栏目:Windows 来源:网络整理
导读:我在我的个人Libs中使用WPF UserControl。 Libs包含在我的WPF和WindowsForms程序中。现在我的UserControl必须显示一个新的(WPF)窗口。在新的窗口我想设置所有者。我这样做: dialog.Owner = Application.Current.MainWindow; 如果我在WPF程序中使用UserContr
我在我的个人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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读