1. Home
  2. Computing & Technology
  3. Visual Basic

Part 2: A First Introduction to WPF and XAML for Visual Basic Programmers
XAML, WPF, and Namespaces

By Dan Mabbutt, About.com

Sep 28 2008

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.

In part 1 of this series, we started with the default XAML file for a WPF application in VB.NET:

<Window x:Class="Window1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="Window1" Height="300" Width="300">
   <Grid>

   </Grid>
</Window>

In part 1, I quoted Microsoft's description of WPF: "The Microsoft Windows Presentation Foundation provides the foundation for building applications." That foundation is in the two namespaces (the second and third lines in the XML code above).

If you aren't familiar with the concept of XML namespaces, it's one of the first keys to learning how to code XAML. It's also key to XML in general. The idea is really very simple: A namespace is a set of names in which all names are unique.

Keep in mind that in this article, we're talking about the "XML Namespace". The term, "namespace" is thrown around pretty loosely these days and it generally means the same thing whereever it's used. The Microsoft website defines a Visual Basic namespace this way:

Namespaces organize the objects defined in an assembly. Assemblies can contain multiple namespaces, which can in turn contain other namespaces.

But "XML Namespace" has very specific rules that apply to XML and XAML that are quite different from the rules that apply to VB.NET namespaces. In VB.NET, the "namespace-dot-class" syntax is used. For example, several objects named "button" exist in .NET. Two of them are:

System.Web.UI.WebControls.Button

and

System.Windows.Forms.Button

But they're in different, unique .NET namespaces.

The problem solved by namespaces (XML and VB) is that, like "button", there are a lot of names that are likely to be used over and over again in code. If you are writing an inventory application for the Amalgamated Shirt Factory, they will likely have an item in their inventory called "button". But it won't be the same as the software, "button". Their namespace might be something like,

xmlns:amal="http://schemas.amalshirts.com/shirtparts".

If a shirt button was intended, the code would read, <amal:button ...>

An example from "the real world" are the names that people have. There might be several people named "John" in a room. So, to avoid confusion, you would have to refer to them with some other addition to their name. "John" from up north. "John" with red hair.

In the same way, the WPF Button is different from the Windows Forms Button. Entirely separate code libraries are used to draw them and define their properties and methods. A WPF Button is in System.Windows.Controls. A Windows forms Button is in System.Windows.Forms.

And finally, XAML is designed so that it could be an "open standard" and maybe some day it will be. The standard would specify how things fit together; in other words, the XAML architecture. Microsoft would still have a completely proprietary implementation and other companies might have different ones. The namespaces above point to the Microsoft implementation. Another company would have their own implementation and namespace.

In fact, XAML is used for more than just WPF in Microsoft. Both WF (WorkFlow Foundation) and Silverlight use XAML to do their magic.

But what do the namespaces mean? On the next page, we cover that.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Part 2: A First Introduction to WPF and XAML for Visual Basic Programmers

©2009 About.com, a part of The New York Times Company.

All rights reserved.