Shahar Nechmad - Blog 2.0

Diaries Of a Startup

  Home  |   Contact  |   Syndication    |   Login
  28 Posts | 0 Stories | 5 Comments | 10 Trackbacks

News



Archives

Other Windows Vista blogs

Windows Vista Essentials

Many customers have asked me about this topic. Customers made big investments in developing libraries of controls in WinForms for their enterprise applications. They want to know, now when they will start to develop Avalon based applications will they need to redevelop all the controls from scratch.
My answer to that question comes in two parts:
  1. if want your windows forms control to take advantage of the new features of Avalon, yes, you will have to rewrite them. Maybe they will be a converter wizard, but I still haven't heard about it and to tell you the truth – I don't really trust converters. Most of the time, you will have to also do manual coding.
  2. The good new are that if you like your controls as they are, you can easily host them inside your Avalon application.
     

So how can you host WinForms controls inside an Avalon Application? Simply follow the next simple steps:

  1. Add a reference to your Avalon Application project, to the windows forms interop assemblies – WindowsFormsIntegration.dll. If you are using WinFX Beta 1, you will find the dll in C:\Program Files\Reference Assemblies\Microsoft\Avalon\v2.0.50215.
  2. Also Add a reference to your WinForms control dll or project
  3. In order to use WinForms integration you must add mappings to the WinFormsIntegration dll to your XAML file:
    <?Mapping XmlNamespace="wfi" ClrNamespace="System.Windows.Forms.Integration"
              Assembly="WindowsFormsIntegration" ?>
    <?Mapping XmlNamespace="mwfc" ClrNamespace="MyWinFormsControls"
     Assembly="MyWinFormsControlsLibrary" ?>
  4. Add your window and panel declarations.
  5. Add a tag for the WinFormHost control. This control lets you host win forms controls in your Avalon applications.
  6. Add your control tag into the Controls collection of the WinFormHost control. Note that Avalon lets you change properties of your win form control using XAML, as if it was a regular Avalon control.

      That's it. You are ready to go. Your complete XAML file should look like this:
      <?Mapping XmlNamespace="wfi" ClrNamespace="System.Windows.Forms.Integration"
                Assembly="WindowsFormsIntegration" ?>
      <?Mapping XmlNamespace="mwfc" ClrNamespace="MyWinFormsControls"
       Assembly="MyWinFormsControlsLibrary" ?>

      <Window x:Class="AvalonHostApp.Window1"
          xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
          xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
          xmlns:wfi="wfi"
          xmlns:mwfc="mwfc"
          Text="MyAvalonHostApplication"
          >
          <Grid>
                <Grid.RowDefinitions>
                       <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <wfi:WindowsFormsHost Grid.Row="0">
                       <wfi:WindowsFormsHost.Controls>
                               <mwfc:WindowsFormsUserControl Dock="Fill" BackColor="Red"> </mwfc:WindowsFormsUserControl>
                      </wfi:WindowsFormsHost.Controls>
                </wfi:WindowsFormsHost>
          </Grid>
      </Window>

    1. For more information about interop between WinForms and Avalon, check the WinFX SDK documentation.

    posted on Monday, August 15, 2005 10:38 PM

    Feedback

    # re: Hosting WinForms controls inside your Avalon application 2/2/2006 7:53 AM V_MS
    Article is good...Could u please tell me how to host a .SWF file in a XAML layout?

    Post A Comment
    Title:
    Name:
    Email:
    Website:
    Comment:
    Verification: