Gotcha: VB.NET classes and namespaces in XAML

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.

A picture of me

Welcome, my name is Paul Stovell. I live in Brisbane and work on Octopus Deploy, an automated deployment tool for .NET applications.

Prior to founding Octopus Deploy, I worked for an investment bank in London building WPF applications, and before that I worked for Readify, an Australian .NET consulting firm. I also worked on a number of open source projects and was an active user group presenter. I was a Microsoft MVP for WPF from 2006 to 2013.