C# – ‘WindowsFormsHost’ was not found

cwpf

In my wpf application i am trying to use windows form control…. but i am getting an error i.e
Error The type 'WindowsFormsHost' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
can any one help me to get it done… the following is my code

       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" />

Best Answer

Normally when we see an error that says:

The type SomeClass was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built

There could be a few problems. Either that means that we didn't add a reference of the relevant dll to our project, or we didn't import the namespace properly.

To find which dll we need to add a reference to, we normally go to MSDN, searching with the search terms 'SomeClass class'... in your case 'WindowsFormsHost class'. Doing this for the WindowsFormsHost class, we see this:

enter image description here

Note the line that says:

Assembly: WindowsFormsIntegration (in WindowsFormsIntegration.dll)

Looking in the Add Reference dialog in Visual Studio, we can see a dll entry for WindowsFormsIntegration:

enter image description here

So that's how we find out which dll to import. Now, we just need to make sure that we import the namespace properly, either in C#:

using System.Windows.Forms.Integration;

In XAML, you do not need to add an XML Namespace for the WindowsFormsIntegration dll before using the WindowsFormsHost control.

<WindowsFormsHost>
    <!-- Some WinForms Control -->
</WindowsFormsHost>

See the WindowsFormsHost Class page on MSDN for more information.