VB.NET • Mix and Match WinForms and Avalon Controls

Listing 3. Displaying Avalon controls in a Windows Form requires members of the MSAvalon[System].Windows.Forms.Integration namespace in Sub Main to hook up Avalon components with procedural code in the Form1_Load event handler that substitutes for their XAML definition. This code adds an ElementHost to a WinForms panel, then adds a child DockPanel to contain the Avalon Button and TextBox controls. This code is a slightly modified version of the PDC Hands-On Labs' HOL-010 example.

'Avalon classes (map to System.Windows)
Imports MSAT = MSAvalon.Threading
Imports MSAW = MSAvalon.Windows
Imports MSAWM = MSAvalon.Windows.Media
Imports MSAWC = MSAvalon.Windows.Controls

'Avalon/Windows Forms integration classes
Imports MSAvalon.Windows.Forms.Integration

Public Class Form1
   Inherits System.Windows.Forms.Form
   'This is a modified version of the PDC's
   'Hands-On Lab 010 code for WindowsFormsApp

#Region " Windows Form Designer generated code "

   Public Shared Sub Main()
      'Set up context and register dispatcher
      Dim context As New MSAT.UIContext
      Dim dispatcher As New MSAW.HwndDispatcher
      dispatcher.RegisterContext(context)
      context.Enter()
      dispatcher.Attach()
      'Run the app
      Application.EnableVisualStyles()
      Application.Run(New Form1)

      'Shutdown the Avalon dispatcher
      dispatcher.Detach()
      context.Exit()
   End Sub

   Private WithEvents AvalonButton1 As _
      MSAWC.Button

    Private Sub Form1_Load(ByVal sender As _
      System.Object, ByVal e As _
      System.EventArgs) Handles MyBase.Load
      Me.Text = "Modified Version of HOL-010"
      Me.StartPosition = _
         FormStartPosition.CenterScreen

      'Add an ElementHost
      Dim ElementHost1 As New ElementHost
      ElementHost1.BackColor = Color.Red
      ElementHost1.Dock = DockStyle.Fill
      Panel1.Controls.Add(ElementHost1)

      'Add Avalon DockPanel to ElementHost
      Dim AvalonPanel1 As New MSAWC.DockPanel
      AvalonPanel1.Background = _
         MSAWM.Brushes.BurlyWood
      ElementHost1.AddChild(AvalonPanel1)

      'Add Avalon Button to DockPanel
      AvalonButton1 = New MSAWC.Button
      With AvalonButton1
         Width = New MSAW.Length(120)
         Height = New MSAW.Length(50)
         Background = MSAWM.Brushes.Coral
         Content = "Avalon Button"
         FontSize = New MSAW.FontSize(10, _
            MSAW.FontSizeType.Point)
            FontWeight = MSAW.FontWeight.Bold
      End With
      AvalonPanel1.Children.Add(AvalonButton1)

      'Add Avalon Text Box to DockPanel
      Dim AvalonText1 As New MSAWC.TextBox
      With AvalonText1
         Width = New MSAW.Length(130)
         HorizontalAlignment = _
            MSAW.HorizontalAlignment.Center
         Text = "Avalon text box"
      End With
      AvalonPanel1.Children.Add(AvalonText1)
    End Sub

   Private Sub Button1_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles _
      Button1.Click
      MessageBox.Show("Hello from Windows Forms")
    End Sub

   Private Sub AvalonButton1_Click( _
      ByVal sender As Object, ByVal e As _
      MSAvalon.Windows.Controls.ClickEventArgs) 
      Handles AvalonButton1.Click
      MessageBox.Show("Hello from Avalon")
   End Sub
End Class