从WPF应用程序中打开WinForm的?应用程序、WPF、WinForm

2023-09-04 01:14:15 作者:Sweet

我已经创建了一个WPF和WinForm的应用程序,我需要做的是 打开从WPF应用程序的WinForm的。两者都是以相同的解决方案,但它们是diferent项目

I've created an WPF and WinForm Application, what i need to do is open the WinForm from the WPF application. Both are in the same solution but they're diferent projects.

我试过如下:

Dim newWinForm as New MainWindow
newWinForm.show()

我发现了一个可能的解决方案从这里: Opening从WPF应用程序的WinForm编程

I found a possible solution from here: Opening winform from wpf application programmatically

但我不明白究竟是什么我必须做的。我希望你能帮助我。谢谢!

But i dont understand what exactly i have to do. I hope you could help me. Thanks!

推荐答案

通常你需要在一个的 WindowInteropHelper , 像在WPF窗口Button.Click事件处理程序如下:

Generally you need to host your form in a WindowInteropHelper, like following in the WPF window Button.Click event handler:

C#:

private void button1_Click(object sender, RoutedEventArgs e) {
  Form1 form = new Form1();
  WindowInteropHelper wih = new WindowInteropHelper(this);
  wih.Owner = form.Handle;
  form.ShowDialog();
}

VB:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim form As New Form1()
    Dim wih As New WindowInteropHelper(Me)
    wih.Owner = Form.Handle
    form.ShowDialog()
End Sub

当然,你需要添加你的项目和System.Windows.Forms.dll中

And of course you need to add reference/import of your project and System.Windows.Forms.dll