Prev | Current Page 149 | Next

Matthew MacDonald

"Pro WPF with VB 2008: Windows Presentation Foundation with .NET 3.5"

MainWindow = New Window1()
app.MainWindow.ShowDialog()
End Sub
End Sub
Code and Uncompiled XAML
One of the most interesting ways to use XAML is to parse it on the fly with the XamlReader. For
example, imagine you start with this XAML content in a file named Window1.xml:



At runtime, you can load this content into a live window to create the same window
shown in Figure 2-3. Here??™s the code that does it:
Imports System.IO
Imports System.Windows.Markup
Public Class Window1
Inherits Window
CHAPTER 2 n XAML 52
Private WithEvents button1 As Button
Public Sub New()
InitializeComponent()
End Sub
Private Sub InitializeComponent()
' Configure the form.
Me.Height = 285
Me.Width = Me.Height
Me.Top = 100
Me.Left = Me.Top
Me.Title = "Dynamically Loaded XAML"
' Get the XAML content from an external file.
Dim s As New FileStream("Window1.xml", FileMode.Open)
Dim rootElement As DependencyObject = CType(XamlReader.Load(s), _
DependencyObject)
Me.Content = rootElement
' Find the control with the appropriate name.
button1 = CType( _
LogicalTreeHelper.FindLogicalNode(rootElement, "button1"), _
Button)
End Sub
Private Sub button1_Click(ByVal sender As Object, _
ByVal e As RoutedEventArgs) Handles button1.


Pages:
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161