【问题标题】:Open WinForm from WPF application?从 WPF 应用程序打开 WinForm?
【发布时间】:2013-05-10 17:42:10
【问题描述】:

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

我尝试了以下方法:

Dim newWinForm as New MainWindow
newWinForm.show()

我从这里找到了一个可能的解决方案: Opening winform from wpf application programmatically

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

【问题讨论】:

  • a Process.Start("Winform.exe"); 可能是另一种方法...
  • 是的,我也这样做了,但是 WPF 应用程序是一个登录表单,所以我认为这不是一个好的选择。谢谢!

标签: .net wpf winforms


【解决方案1】:

通常您需要将表单托管在WindowInteropHelper, 就像在 WPF 窗口 Button.Click 事件处理程序中一样:

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 的引用/导入

【讨论】:

  • 您好,现在当 WinForm 应用程序启动时,它会在这一行引发异常: Dim conexion As New SqlConnection(ConfigurationManager.ConnectionStrings("CONN").ConnectionString) object reference not set to an instance一个对象它是一个实例变量。问候!
  • 这只是意味着您的代码中的某些内容为空或什么都没有。这可能是因为您错过了一些初始化代码来构建连接字符串等。我的示例代码没有创建新进程,因此您的 WinForm 应用程序永远不会启动。如果您想开始一个新流程,请尝试@Jeremy Thompson 的评论Process.Start("Winform.exe");
  • 我刚刚开始工作,但现在我遇到了这个问题:当新的 WinForm 被渲染时,控件看起来像一个 Win98 应用程序,一种旧的外观和感觉。你好!我在 W7
  • 您需要在创建窗口之前通过调用System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); 来启用视觉样式。您可以查看 WinForm 项目中的 Program.cs。干杯!
  • 不,您不需要使用任何互操作或花哨的东西。您只需要从 WPF 中引用 WinForm 项目,创建 WinForm 对象,然后在其上调用Show()
【解决方案2】:

假设你这两个项目分别叫做WPFAppWinFormApp

它们都声明了一个MainWindow 类,它是主应用程序窗口。

要从WPFApp 应用程序中打开WinFormApp MainWindow,您只需对WPFApp 项目执行以下操作:

  1. 添加对WinFormApp项目的引用
  2. 添加对System.Windows.Forms的引用
  3. 创建一个新的WinFormApp.MainWindow 对象
  4. 拨打Show()就可以了

【讨论】:

    【解决方案3】:

    特里的回答对我不起作用。我想返回我的 WPF 窗口,但后来我不得不将窗口的句柄作为参数添加到 ShowDialog() 方法。我可以修好。

    This solution 来自用户 dapi 的类似问题在我的情况下效果更好:

    var winForm = new MyFrm();
    winForm.ShowDialog(new WpfWindowWrapper(Window.GetWindow(this)));
    

    带有一个小助手类:

    public class WpfWindowWrapper : System.Windows.Forms.IWin32Window
    {
        public WpfWindowWrapper(Window wpfWindow)
        {
            Handle = new WindowInteropHelper(wpfWindow).Handle;
        }
    
        public IntPtr Handle { get; }
    }
    

    【讨论】:

      【解决方案4】:

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

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

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

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

      <Grid Name="grid">
      
      </Grid>
      

      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-添加两个对项目的引用:WindowsFormsIntegration, System.Windows.Forms

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 2013-05-24
      相关资源
      最近更新 更多