简体   繁体   English

WPF应用程序上托管的Winforms应用程序

[英]Winforms App hosted on WPF App

I need host a WinForms app on WPF app. 我需要在WPF应用程序上托管WinForms应用程序。 I followed the steps here , but I've an error: 我按照这里的步骤进行操作,但出现错误:

System.Reflection.TargetInvocationException was unhandled Message: Se produjo una excepción en el destino de la invocación. 尚未处理System.Reflection.TargetInvocationException消息:发生意外事件。

What's wrong? 怎么了? I'm using VS2012 and .NET 4.5. 我正在使用VS2012和.NET 4.5。 The WinForms App is only a Form with a Button . WinForms应用程序仅是带有ButtonForm The event click show a MessageBox with a message Hello World nothing more. 事件单击将显示一个消息MessageBox ,其中仅显示一条消息Hello World

I have used WindowsFormsIntegration.dll before and it works fine. 我以前使用过WindowsFormsIntegration.dll,并且工作正常。 this should help you get started. 这应该可以帮助您入门。 Add a reference to WindowsFormsIntegration first. 首先添加对WindowsFormsIntegration的引用。 then... 然后...

using System.Windows.Forms.Integration;

... ...

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var form = new Form1();
        form.TopLevel = false;

        WindowsFormsHost host = new WindowsFormsHost();
        host.Child = form;
        host.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        host.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;


        grid.Children.Add(host);
    }

... ...

<Window x:Class="WpfSandbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded">
    <Grid x:Name="grid">
    </Grid>
</Window>

and now for the simple winform 现在是简单的winform

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

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("hello");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM