I finally got around to installing Visual Studio 2008 this weekend. The first thing I thought I would try to learn something about is Windows Presentation Foundation (WPF). WPF is a graphical subsystem feature of the .NET 3.0 framework that provides a clear separation between UI and Business logic. The mark up language used in WPF is called Extensible Application Markup Language (XAML). XAML looks like any other markup language like HTML and XML.
Below is a sample form built in WPF.
Create new project

Solution Explorer

XAML Designer
Form in Runtime

–
XAML Listing
<Window x:Class="FirstWpf.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Calculate Max Heart Rate" Height="181" Width="370" Background="LemonChiffon"> <Grid Height="141" Width="339"> <Button Margin="89,0,92,13" Name="button1" Click="button1_Click" Height="23" VerticalAlignment="Bottom" FontWeight="Bold" Foreground="DarkGreen">Click Me</Button> <TextBox Height="23" Margin="0,21,115,0" Name="textAge" VerticalAlignment="Top" HorizontalAlignment="Right" Width="36" /> <Label Height="23" Margin="83,21,0,0" Name="label1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="65" FontWeight="Bold" Foreground="DarkBlue">Enter Age</Label> <Label Margin="38,56,35,44" Name="labelHR" FontSize="9" ForceCursor="False" HorizontalContentAlignment="Center"></Label> </Grid> </Window>
C# Code Behind
using System; using System.Windows; namespace FirstWpf { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { try { int age = Int32.MinValue; int maxHr = 0; age = Int32.Parse(textAge.Text); maxHr = 220 - age; labelHR.Content = "At the age of " + age.ToString() + " the Max HR should not rise above " + maxHr.ToString(); } catch (Exception) { labelHR.Content = "Input is not valid"; return; } } } }
WPF/XAML is a feature rich and powerful programming model. In upcoming posts, I will go into deeper detail on some of the great features.
There are a plethora of internet resources for this interesting new technology. Below are just a few of them.
–
Related Content
Windows Client.NET – Official MS Site
Josh Smith on WPF
Karl on WPF
WPF Wonderland
The WPF Blog
Learn WPF
Your XAML Information Page
–
Happy programming!!!
Rhonda,
Nice first WPF post! Isn’t WPF fun!
If you wanted to, you could use a cool feature of WPF called Value Converters. This would allow you to remove button and the event handler and as the user entered the age, the lableHR text would change automatically; displaying an entry error message or the same message you have now.
The calculation code would be moved to the Value Converter class and you would add a Binding statement to the labelHR in your XAML markup.
Wishing you a bright WPF future!
Cheers,
Karl
By: Karl Shifflett on February 24, 2008
at 11:49 pm
Thanks Karl. I will definitely look at that.
By: Rhonda Tipton on February 25, 2008
at 8:42 am
[...] VS 2008 and WPF (Rhonda Tipton) [...]
By: Daily Bits - February 25, 2008 | Alvin Ashcraft's Daily Geek Bits on February 25, 2008
at 9:34 am