从 WPF 应用程序打开 WinForm?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16577122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:48:15  来源:igfitidea点击:

Open WinForm from WPF application?

.netwpfwinforms

提问by Emmanuel Santana

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.

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

I tried the following:

我尝试了以下方法:

Dim newWinForm as New MainWindow
newWinForm.show()

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

我从这里找到了一个可能的解决方案:以 编程方式从 wpf 应用程序打开 winform

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

但我不明白我到底要做什么。我希望你能帮助我。谢谢!

回答by terry

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

通常,您需要在WindowInteropHelper 中托管您的表单,如下面的 WPF 窗口 Button.Click 事件处理程序:

C#:

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:

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

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

当然,您需要添加项目和 System.Windows.Forms.dll 的引用/导入

回答by majid.moazzeni

it is impossible loading a win form from WPF application. so you can do like this:

不可能从 WPF 应用程序加载 win 表单。所以你可以这样做:

1- Create a User control in winform project and add all form's element to user control

1-在winform项目中创建一个用户控件并将所有表单元素添加到用户控件中

public partial class myUserControl : UserControl, IDisposable 
{
...// All Form Code and element put here
}

2- create a wpf window and put a Grid into that:

2- 创建一个 wpf 窗口并将一个 Grid 放入其中:

<Grid Name="grid">

</Grid>

3- on Wpf window Code behind like this :

3- 在 Wpf 窗口后面的代码如下:

public partial class myWpfWindow: Window
{
    public myWpfWindow()
    {
        InitializeComponent();

        myUserControl = new myUserControl ();
        System.Windows.Forms.Integration.WindowsFormsHost winformHost = new 
             System.Windows.Forms.Integration.WindowsFormsHost();
        winformHost.Child = myUserControl;

        grid.Children.Add(winformHost);  // --> <Grid Name="grid">

    }

}

4- add two reference to project:WindowsFormsIntegration, System.Windows.Forms

4- 添加两个对项目的引用:WindowsFormsIntegration, System.Windows.Forms