gpt4 book ai didi

c# - 如何使用反射在 Winform 中托管 WPF 应用程序

转载 作者:太空宇宙 更新时间:2023-11-03 10:56:58 25 4
gpt4 key购买 nike

我有一个简单的 WPF 应用程序 MyWPF.exe,

namespace MyWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

public MainWindow()
{
InitializeComponent();

}

private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button is clicked");
}
}
}

我想在我的 Winform 应用程序中加载这个“MainWindow”,我试过如下,当创建实例时出现异常 “System.InvalidOperationException: Window must be the root of the tree. Cannot add Window as视觉的 child 。”

        String path = @"D:\Test\MyForm\MyWPF\bin\Debug\MyWPF.exe";
System.Reflection.Assembly myExeApp = System.Reflection.Assembly.LoadFile(path);
System.Type MyWPFType = myExeApp.GetType("MyWPF.MainWindow");

var MyWPFInstance = (System.Windows.Window)myExeApp.CreateInstance("MyWPF.MainWindow");

ctrlHost = new ElementHost();
ctrlHost.Dock = DockStyle.Fill;
ElementHost.EnableModelessKeyboardInterop(MyWPFInstance );
ctrlHost.Child = MyWPFInstance ;
this.Controls.Add(ctrlHost);

有可能还是我遗漏了什么?整个异常如下,

System.InvalidOperationException: Window must be the root of the tree. Cannot add Window as a child of Visual.
at System.Windows.Window.OnAncestorChanged()
at System.Windows.FrameworkElement.OnAncestorChangedInternal(TreeChangeInfo parentTreeState)
at System.Windows.TreeWalkHelper.OnAncestorChanged(DependencyObject d, TreeChangeInfo info)
at System.Windows.DescendentsWalker`1.StartWalk(DependencyObject startNode, Boolean skipStartNode)
at MS.Internal.PrePostDescendentsWalker`1.StartWalk(DependencyObject startNode, Boolean skipStartNode)
at System.Windows.TreeWalkHelper.InvalidateOnTreeChange(FrameworkElement fe, FrameworkContentElement fce, DependencyObject parent, Boolean isAddOperation)
at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
at System.Windows.FrameworkElement.AddLogicalChild(Object child)
at System.Windows.Controls.UIElementCollection.AddInternal(UIElement element)
at System.Windows.Controls.UIElementCollection.Add(UIElement element)
at System.Windows.Forms.Integration.ElementHost.set_Child(UIElement value)
at MyForm.Form1.button1_Click(Object sender, EventArgs e) in D:\Test\MyForm\MyForm\Form1.cs:line 37
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

最佳答案

ElementHost 不适用于这种互操作。您可以使用任何非 Window 控件作为其子控件。您可以重构 WPF 应用,使主窗口包含一个用户控件,您将使用反射代码加载该控件并将其添加到窗体。

或者,您可以使用 Win32 SetParent 将 WPF 窗口放置为 Windows 窗体控件的子控件,但我绝对会避免这种情况。

关于c# - 如何使用反射在 Winform 中托管 WPF 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19160289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com