ElementHost 中的 Application.Current 为空 [英] Application.Current in ElementHost is null

查看:27
本文介绍了ElementHost 中的 Application.Current 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的个人库中使用 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 为空?

解决方案

Application.Current 特定于 WPF 应用程序.
因此,当您在 WinForms 应用程序中使用 WPF 控件时,您需要初始化 WPF 应用程序的实例.在您的 WinForms 应用程序中执行此操作.

if ( null == System.Windows.Application.Current ){新 System.Windows.Application();}

I use a WPF UserControl in my personal Libs. The Libs are included in my WPF and WindowsForms programs. Now my UserControl has to show a new (WPF) Window. At the new Window I want to set the Owner. I do it like this:

dialog.Owner = Application.Current.MainWindow;

This works fine, if i use the UserControl in a WPF program.

When I use the UserControl in my WindowsForms program (I set the UserControl in a ElementHost elementHost.Child = ...) is Application.Current null.

This is not good and my program throws an exception.

Why is Application.Current null?

解决方案

Application.Current is Specific for WPF Application.
Therefore when you are using WPF controls in WinForms Application you need to initialize instance of WPF Application. Do this in your WinForms Application.

if ( null == System.Windows.Application.Current )
{
   new System.Windows.Application();
} 

这篇关于ElementHost 中的 Application.Current 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆