Accessing local assemblies in XAML

By | July 28, 2011

If you are new to WPF, you must know that XAML can define or declare any object under your local / global namespace just like your code does. It has full capability to load up your types based on the namespace you provide in your XAML.

Lets look how you can load your own namespace in XAML.

<Window x:Class="TestImagesApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="418" Width="530">
</Window>

This is the basic XAML that appear from template when you just create a new project. Now if you see minutely, you can see xmlns is there which points to some url of presentation layer. This will ensure that all the references of WPF objects is itself available to your XAML and you do not need to refer its namespace. Now what if you need to use something other than this ? As I told you that XAML can load any external type. You need to add a namespace to the XAML, which will instruct the Loader to load the CLR assembly and expose the Type that you use in the XAML.
For simplicity lets add a string as resource in the Window.

<Window x:Class="TestImagesApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="418" Width="530">
    <Grid>
        <Grid.Resources>
            <System:String x:Key="strString">This is a string</System:String>
        </Grid.Resources>
        <TextBox Text="{StaticResource strString}" />
    </Grid>
</Window>

So here the xmlns:System will load the assembly mscorlib and expose the namespace System to your XAML. Clr-namespace :System specifies the namespace and assembly=mscorlib refer the assembly. You should remember that the assembly should exist on GAC or local bin folder to make sure the XAML can load it properly.
After you load the assembly, you can refer to the Types as shown in the XAML, the textbox loads up the string as text from Resource.
Similar to this, you can also load your own custom type and use it in XAML.
Lets define a class :

namespace MyNamespace.Extension
{
    public class MyClass
    {
        public string X { get; set; }

        public string Y { get; set; }
    }
}

Now to refer to this class in XAML you can use :

<Window x:Class="TestImagesApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:MyNamespace.Extension"
        Title="MainWindow" Height="418" Width="530">
    <Grid>
        <Grid.Resources>
            <System:String x:Key="strString">This is a string</System:String>
            <local:MyClass X="Value of X" Y="Value of Y" x:Key="myobject"/>
        </Grid.Resources>
    </Grid>
</Window>

Thus you can see I can refer to my own type using clr-namespace attribute. You can specify the assembly here too, but it is not mandatory, as when you do not specify it, it will point to the local assembly.
Local:MyClass can now refer to my Type with X and Y properties exposes as well.

I hope this will help you.
Thanks for reading.