Home > English > Silverlight 4 and Xml namespaces

Silverlight 4 and Xml namespaces

Probably one of the most irritating issues when dealing with Xaml in Silverlight was the pain of creating all the Xmlns namespace entries for the namespaces and assemblies you wanted to use in your Xaml.  The following is an example of what you would have to do in Sivlerlight 3:

<UserControl
x:Class="Designer.FormView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:radGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
xmlns:radDock="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking"
xmlns:radNav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
xmlns:radDrag="clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls"
xmlns:radPrimitives="clr-namespace:Telerik.Windows.Controls.Primitives;assembly=Telerik.Windows.Controls"
xmlns:dc="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:ctls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input"
xmlns:navMap="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
xmlns:nav="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
>
...
</UserControl>

Now in Silverlight 4, you can now do the following:

<UserControl
x:Class="Designer.FormView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:rad="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
>
...
</UserControl>

So if you want the same features and power in your own library, all you need to do is provide the XmlnsDefinition attribute. This attribute lives in the System.Xaml assembly.

If you would like to do this to your own assemblies, you would something like to the following to your AssemblyInfo.cs file:

[assembly: XmlnsDefinition(Constants.XmlNamespace, "Core.UI.View")]
[assembly: XmlnsDefinition(Constants.XmlNamespace, "Core.UI.Controls")]
[assembly: XmlnsDefinition(Constants.XmlNamespace, "Core.UI.Interactivity")]


[assembly: XmlnsPrefix(Constants.XmlNamespace, "core")]

internal static class Constants
{
    internal const string XmlNamespace = "http://schemas.mywebsite.com/core";
}

Finally, you would access you new namespace definitions with the following:

...
xmlns:core="http://schemas.mywebsite.com/core"
...

With this firmly tucked in your toolbelt, you are ready to make your developer’s lives much easier and nicer to deal with. If you have multiple assemblies, just use the same XmlNamespace so that they will all show up correctly.

Hope this helps…

Advertisements
Categories: English Tags: , ,
  1. corneliu
    September 14, 2010 at 7:52 pm

    Hi,

    This seems very nice but for some reason seems to not work on 2 enviroments I have in office. I just place a radGridView in Blend 4 and when I hit F5 or try to build I get this message: “The tag RadGridView does not exist in XML namespace assembly-Telerik-Windows-Controls-GridView “clr namespace Telerik Windows Controls”.
    I have added the namespaces like suggested but same behaviour. If I open same project in VS 2010 it compiles with no problem. References are added for all required Telerik dlls
    Telerik.Windows.Controls,
    Telerik.Windows.Controls.Data
    Telerik.Windows.Controls.GridView
    Telerik.Windows.Controls.Input
    Telerik.Windows.Data
    But for some reason it doens’t want to work in blend but only in VS 2010.

    Any suggestion will be very appreciated.
    10x

    • September 25, 2010 at 12:23 pm

      Corneliu,

      I will do a sample in Blend and see what I get and report back.

      Regards,

      Matt

  2. January 11, 2013 at 8:29 pm

    Many thanks for spending time to create “Silverlight 4 and Xml namespaces Matt Duffield”.
    Thanks once more -Andrea

  3. January 17, 2013 at 7:58 pm

    “Silverlight 4 and Xml namespaces Matt Duffield” in fact enables me
    ponder a tiny bit more. I treasured each and every particular portion of this blog post.
    Regards -Melody

  4. January 23, 2013 at 8:33 am

    I Believe that blog, “Silverlight 4 and Xml namespaces Matt Duffield” was in fact just
    right! I personallycouldn’t see eye to eye along with u even more! Finally seems like I reallycame across a blog website very well worth looking through. Many thanks, Rickey

  1. No trackbacks yet.

Leave a Reply

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

%d bloggers like this: