简体   繁体   English

如何从WinForms ElementHost中托管的WPF UserControl获取对Window的引用

[英]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.

相关问题 如何在 Winforms 窗体和由 Winforms 窗体中的 elementhost 托管的 WPF 用户控件之间使用委托? - How to use a delegate between a Winforms Form and a WPF Usercontrol hosted by an elementhost in the Winforms form? 从C#Winforms上的elementhost中托管的RichText wpf控件获取文本 - Get text from RichText wpf control hosted in elementhost on Winforms in C# 如何使用KeyPresses在不带窗口的ElementHost中托管WPF用户控件 - How to host a WPF UserControl in ElementHost without Window with KeyPresses 如何在Winforms / WPF中创建透明ElementHost - How to create Transparent ElementHost's in Winforms/WPF 如何在XAML中为WPF中托管的Winforms UserControl解析自定义属性名称? - How To Resolve Custom Property Name In XAML For a Winforms UserControl Hosted In WPF? 如何从自定义WPF窗口获取结果到UserControl中的父窗口 - How to get result from custom WPF window to parent window in UserControl 将ElementHost调整为托管XAML UserControl的大小 - Resize ElementHost to size of the hosted XAML UserControl WPF DependencyProperty未在WPF UserControl(ElementHost)中更新 - WPF DependencyProperty not updating in WPF UserControl (ElementHost) WinForms和WPF UserControl显示另一个窗口 - WinForms with WPF UserControl which shows another window 如何从 WPF 中的另一个 UserControl 绑定或引用 UserControl? - How to bind or reference a UserControl from another UserControl in WPF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM