它是绝对安全的,显示从一个WinForms形式WPF窗口?它是、形式、窗口、安全

2023-09-07 09:57:36 作者:明了

我想从一个Windows窗体应用程序(.NET 3.5)显示一个WPF窗口。

I would like to display a WPF window from a windows forms application (.NET 3.5).

这code似乎没有在样本项目中的任何问题工作:

This code seems to work without any problem in a sample project:

public partial class WinFormsForm1 : Form
{
    public WinFormsForm1() {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
      WpfWindow1 w = new WpfWindow1();
      w.Show();
    }
}

的形式从开始的Main()作为一个正常的WinForms形式:

The form is started from Main() as a normal Winforms form:

Application.Run(new WinFormsForm1());

这对我来说太容易是真实的。有没有在这有什么缺点?这是安全的吗?

This seems to me too easy to be true. Are there any shortcomings in this? Is this safe to do?

推荐答案

它有一个严重的缺陷:无模式的WPF窗口将not获取键盘输入。

It has one serious shortcoming: the modeless WPF window would not get keyboard input.

EnableModelessKeyboardInterop 方法调用所需要的WPF窗口前添加所示:

The EnableModelessKeyboardInterop method call needs to be added before the WPF window is shown:

  WpfWindow1 w = new WpfWindow1();
  System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(w);
  w.Show();

ElementHost的resides在WindowsFormsIntegration.dll程序的。

延伸阅读:http://msdn.microsoft.com/en-us/library/aa348549.aspx