Application.Current in ElementHost is null

假如想象 提交于 2019-11-29 16:59:22

问题


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?


回答1:


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();
} 


来源:https://stackoverflow.com/questions/12198244/application-current-in-elementhost-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!