我有一个简单的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必须是树的根。不能将Window添加为视觉的孩子。“
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)
答案 0 :(得分:2)
ElementHost
不适用于此类互操作。您可以将任何非Window
控件用作其子级。您可以重构您的WPF应用程序,以便主窗口包含仅用户控件,您将使用反射代码加载该用户控件并添加到表单中。
或者,您可以使用Win32 SetParent
将WPF窗口作为Windows窗体控件的子窗口放置,但我绝对会避免这种情况。
答案 1 :(得分:0)
我知道,这是一个古老的问题,但是也许其他人也在寻找解决方案。
只需放置以下行:
GoogleService-Info.plist
在每个WPF窗口的ElementHost.EnableModelessKeyboardInterop(this);
之前。
它帮助了我。