2014-07-11 56 views
2

在我的wpf應用程序中,我嘗試使用Windows窗體控件....但我遇到錯誤,例如 錯誤未找到類型「WindowsFormsHost」。驗證您是否缺少程序集引用,並且所有引用的程序集都已構建。 任何一個可以幫助我把它做...以下是我的代碼'WindowsFormsHost'未找到

 c#:code 

    public Window2() 
    { 
     InitializeComponent(); 
     System.Windows.Forms.PictureBox PictureBox1 = new System.Windows.Forms.PictureBox(); 
     windowsFormsHost1.Child = PictureBox1; 
     PictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(PictureBox1_Paint); 


    } 
    xaml:code 


     <WindowsFormsHost Height="175" HorizontalAlignment="Left" Margin="10,10,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="255" /> 
+0

您確定已經包含此'xmlns =「http://schemas.microsoft.com/winfx/2006/xaml/presentation」' 和'xmlns:x =「http://schemas.microsoft.com/ winfx/2006/xaml「' –

+0

雅我添加了這些引用... – user3413736

+0

你有使用'WindowsFormsIntegration' ref? – Rang

回答

10

通常情況下,當我們看到一個錯誤,指出:

類型SomeClass沒有被發現。確認您沒有遺漏裝配參考,並且所有參考裝配都已建成

可能有幾個問題。這意味着我們沒有將相關dll的引用添加到我們的項目中,或者我們沒有正確導入名稱空間。

要找到我們需要添加引用的dll,我們通常會轉到MSDN,搜索搜索詞'SomeClass class'...在您的案例中'WindowsFormsHost class'。這樣做的WindowsFormsHost類中,我們看到:

enter image description here

注意這行:

大會:WindowsFormsIntegration程序(在WindowsFormsIntegration.dll程序)

在展望在Visual Studio中添加引用對話框,我們可以看到一個dll條目爲WindowsFormsIntegration

enter image description here

所以這就是我們如何找出哪個dll導入。現在,我們只需要確保我們正確導入的命名空間,無論是在C#:

using System.Windows.Forms.Integration; 

在XAML中,你不需要使用WindowsFormsHost控制前添加一個XML命名空間爲WindowsFormsIntegration DLL。

​​

請參閱WindowsFormsHost MSDN上的Class頁面以獲取更多信息。