If you haven't looked at part 1 of this series, you might want to go back and check it out first. Also, all code here is based on VB.NET 2008 Express, to be certain that you can use totally free software.
Up to this point in the WPF and XAML series, we haven't actually written any VB code because XAML is such a fundamental change. We needed to establish a foundation in that technology first. In this part of the series, that will change!
The term "event model" means the preplanned way that your code works with Framework code. For example, you know that you can code a subroutine that "handles" a click event. In WPF, there are big changes in the "event model" too.
To begin, there are two places that you can code events now. There is one .vb file for application level events in Application.xaml.vb such as Startup and Exit and another for component events like handling button clicks in windowname.xaml.vb (where windowname is determined in your project).
In addition, although some of the components may seem familiar, they're loaded with new methods and properties. For example, the WPF Button component has a three-way ClickMode property: Press, Release, and Hover. The new Button is designed for the web!
But one of the biggest changes is the completely new way that your VB.NET code will respond to events. Most of this lesson is about that. It's called routed events because the same event can be routed to a lot of subroutines, not just one.
The first thing to make sure you understand is what an "object" is in the XAML code and how it corresponds to the VB.NET code.In general, each element in the XAML code corresponds to an object. Each attribute corresponds to a property. The terms "element" and "attribute" are XML terms since XAML is a type of XML. In the example below, Button is an element and Name, Height, Margin, and VerticalAlignment are attributes.) This rule isn't absolute, but it's close and it's important that you understand the concept. For example, the XAML code ...
<Button
Name="Button1"
Height="50"
Margin="100,100,100,0"
VerticalAlignment="Top">
Button1
</Button>
... is essentially the same as the VB.NET code ...
Dim Button1 As New Button
Button1.Height = 50
Button1.Margin = New Thickness(100, 100, 100, 0)
Button1.VerticalAlignment = Windows.VerticalAlignment.Top
Button1.Content = "Button1"
myGrid.Children.Add(Button1)
Just to make sure that you understand the terms you will see in documentation, writing XAML to do something is often referred to as declarative code. The way we have always done it in VB is called programmatic code. When either the XAML code or the VB.NET code is compiled, it's turned into IL (Intermediate Language) code. The XAML code and the VB code are merged as "partial classes" to create the assembly for the solution.
To demonstrate that they're essentially equal, the illustration below shows both a Button created using XAML and a Button created using VB.NET together in the same running code:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
This is the structure of a WPF VB.NET program. On the next page, we take a look at how event subroutines are called using the new WPF Routed Events model.
