Tip: VB.NET XAML Classes and Namespaces

One difference between XAML in a C# project and VB.NET projects is in specifying the x:Class of your XAML root element. For example, in a C# Window XAML file:

<Window
    x:Class="BigBank.UI.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>

By contrast, the VB.NET version must leave out the namespace:

<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>

If you forget to make these changes, a typical error message you might encounter is "Name ‘InitializeComponent’ is not declared", or similar. This occurs because the generated VB.NET file to load the XAML is placed into a namespace different to the one of your code-behind, and thus the code behind can’t find it’s partial class which declares the method.

Where this becomes inconsistent is when you include other namespaces in your project, whereby you do need to use the fully qualified namespace:

<Window
    x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:me="clr-namespace:BigBank.UI.Controls"
    Title="Window1" Height="300" Width="300"
    >
    <Grid>

    </Grid>
</Window>

This issue typically comes up when converting VB.NET XAML files to C# projects or vice-versa. I’m used to the VB.NET language being woefully inconsistent, but now that inconsistency is leaking into my XAML? :(

6 Responses to “Tip: VB.NET XAML Classes and Namespaces”

  1. You get a similar error if the:

    entry is missing from the csproj file. Something to watch out for.

  2. Hi Paul,

    Just encounter a namespace issue in a VB.NET silverilght project. I think they alligned the behaviour of namespaces in Silverilght so that VB.NET and C# behaves the same way.

    Your examples are for WPF, right?

  3. Actually Paul it is consistent. The root namespace is declared at project level in the project properties.

  4. ” I’m used to the VB.NET language being woefully inconsistent”

    i know you’re frustrated but…..give me a break.

  5. @Jonas, correct, just for WPF. It’s bothersome that they changed it in Silverlight though - now even the XAML is inconsistent :(

    @Bill, I know the reason, but I think the VB.NET generator for XAML should assume the x:Class definition is fully qualified, as it does for imports (it doesn’t import namespaces from your project file’s Imports section).

    @Gabe, just seeing who will bite :)

  6. […] public links >> xaml Developing Silverlight 1.0 RIAs: XAML Saved by realcissy on Wed 01-10-2008 VB.NET XAML Classes and Namespaces Saved by cavemanlawyer15 on Tue 30-9-2008 Using FlowDocuments XAML to print XPS Documents. (Part […]

Leave a Reply