Note to self – mapping of external namespaces into XAML files changed

I stumbled upon this yesterday. If an external class needs to be referenced in a XAML file, a namespace must be mapped for it, so the compiler knows where to find that class. For example, I had a DataTemplate for a specific type in my file:

  <DataTemplate DataType="{x:Type arbitraryName:Klasse}">
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding Path=Name}" />
      <TextBlock Text=", Jahrgang " />
      <TextBlock Text="{Binding Path=Anfangsjahr}" />
    </StackPanel>
  </DataTemplate>

Now, the class Klasse is part of a certain namespace (Sturm.MyNamespace), in a certain assembly (Sturm.MyDataAssembly.dll), and in the many references I was able to find, it said I should have something like this in my XAML file for the mapping of the “arbitraryName” namespace:

<?Mapping XmlNamespace="arbitraryName" ClrNamespace="Sturm.MyNamespace" Assembly="Sturm.MyDataAssembly" ?>
...
<Window ...
  xmlns:arbitraryName="arbitraryName"
  ... >

The problem was that this didn’t seem to work for me – I got errors all the time saying the type “arbitraryName:Klasse” couldn’t be cast to a Type object. Finally I found the solution somewhere: Microsoft changed the syntax of this, a few months back apparently, but obviously not long enough ago judging from the number of web pages giving the old syntax. The new format doesn’t need (and actually doesn’t seem to make any use of) the Mapping instruction, and the xmlns entry has to look like this:

...
<Window ...
  xmlns:arbitraryName="clr-namespace:Sturm.MyNamespace;assembly=Sturm.MyDataAssembly"
  ... >

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s