2013-10-03 67 views
2

我有一個簡單的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應用程序加載這個「主窗口」在WinForm的主機WPF應用程序,我試圖像下面,我得到當創建實例爲「」時發生異常「System.InvalidOperationException:窗口必須是樹的根,不能將Window添加爲Visual的子項。」

 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

這聽起來像你不能把一個窗口內的控件。似乎很合理。爲什麼不使用其他類型的容器而不是向控制主機添加*窗口?網格或其他面板可能? – sircodesalot

+0

但我可以在Winform裏面用Winform做到這一點。 – Dev

+0

我當然不懷疑,但是如果你打算完全重新設計一個API(比如WPF),你可能會考慮約束那些在技術上沒有意義的東西(比如把窗口放在窗口內)。此外,如果它不允許你使用Window,你可以改變你的窗口從'UserControl'(而不是'Window')派生,並且解決問題。 – sircodesalot

回答

2

ElementHost不適用於這種互操作。您可以使用任何非Window控件作爲其子。您可以重構您的WPF應用程序,使主窗口包含只有用戶控件,您將使用反射代碼加載並添加到窗體。

或者,您可以使用Win32 SetParent將WPF窗口作爲Windows窗體控件的子窗口,但我絕對會避免這種情況。

+0

我只想嘗試,以及如何做到這一點? – Dev