[英]Winforms App hosted on WPF App
我需要在WPF應用程序上托管WinForms應用程序。 我按照這里的步驟進行操作,但出現錯誤:
尚未處理System.Reflection.TargetInvocationException消息:發生意外事件。
怎么了? 我正在使用VS2012和.NET 4.5。 WinForms應用程序僅是帶有Button
的Form
。 事件單擊將顯示一個消息MessageBox
,其中僅顯示一條消息Hello World
。
我以前使用過WindowsFormsIntegration.dll,並且工作正常。 這應該可以幫助您入門。 首先添加對WindowsFormsIntegration的引用。 然后...
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>
現在是簡單的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.