[英]How to get a reference to the Window from a WPF UserControl hosted in a WinForms ElementHost
I have a WPF UserControl that is being hosted on a Windows Form via an ElementHost. 我有一个WPF UserControl,通过ElementHost托管在Windows窗体上。 Within the WPF UserControl there is a button for editing Items details that opens a new WPF Window.
在WPF UserControl中,有一个用于编辑项目详细信息的按钮,用于打开新的WPF窗口。
EditAlarm view = new EditAlarm();
view.DataContext = viewModel;
view.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
view.ShowDialog();
This works the first time and displays the window. 这是第一次工作并显示窗口。 Once the window is closed however clicking the button again throws an error on:
窗口关闭后,再次单击该按钮会引发错误:
EditAlarm view = new EditAlarm();
The error given is "The Application object is being shut down." 给出的错误是“应用程序对象正在关闭”。 I believe this is because the Application does not have a MainWindow set, so when the Edit Window is displayed it believes it to be the MainWindow so that closing it makes it think the Application is closing too.
我相信这是因为应用程序没有设置MainWindow,因此当显示编辑窗口时,它认为它是MainWindow,因此关闭它会让它认为应用程序也正在关闭。
To fix this I as going to try the following: 为了解决这个问题,我将尝试以下方法:
// In UserControl Constructor
var window = System.Windows.Window.GetWindow(this);
System.Windows.Application.Current.MainWindow = window;
or 要么
// In Windows Form
var window = System.Windows.Window.GetWindow(view);
System.Windows.Application.Current.MainWindow = window;
However the window reference is always null. 但是,窗口引用始终为null。 How do I get a reference to WindowsForm Window to use as the MainWindow in the WPF app?
如何获取WindowsForm窗口的引用以用作WPF应用程序中的MainWindow? For a bonus, will this address my issue with trying to open and close new WPF windows inside my WPF Usercontrol?
要获得奖励,这是否会解决我在WPF Usercontrol中打开和关闭新WPF窗口的问题?
Thanks! 谢谢!
I ran across this recently despite the fact that I had already explicitly created a Windows.Application
object in the constructor of my main form. 尽管我已经在我的主窗体的构造函数中显式创建了一个
Windows.Application
对象,但我最近遇到了这个问题。 Your theory is correct. 你的理论是正确的。 The
Application
object has a property, ShutdownMode
, with a default of OnLastWindowClose
. Application
对象有一个属性ShutdownMode
,默认值为OnLastWindowClose
。 I changed the value to OnExplicitShutdown
and the problem went away. 我将值更改为
OnExplicitShutdown
,问题就消失了。 Here's the code from my WinForm in VB. 这是我在VB中使用WinForm的代码。
Private Property WPFApplication As Windows.Application
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
If (WPFApplication Is Nothing) Then
WPFApplication = New Windows.Application
WPFApplication.ShutdownMode = Windows.ShutdownMode.OnExplicitShutdown
End If
End Sub
Private Sub frmMain_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If (WPFApplication IsNot Nothing) Then
WPFApplication.Shutdown()
End If
End Sub
You can set the owner of the wpf window with the WindowInteropHelper class 您可以使用WindowInteropHelper类设置wpf窗口的所有者
var helper = new WindowInteropHelper(window);
helper.Owner = ownerForm.Handle;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.