Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there XAML namespaces that are URLS?

In Silverlight/WPF xaml at the top of the code you have your namespace/import type declarations. I can easily understand how these declarations can point to an assembly so that the types etc can be loaded from it. What I don't understand (and what I haven't really thought about up to now) is how these namespaces work when they point to a url, e.g.

http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit

Looking at this URL gives me an error so doesn't tell me anything.

like image 655
Calanus Avatar asked Mar 22 '11 10:03

Calanus


1 Answers

There's an attribute you can use in the code of your referenced assembly that maps a Uri to your code namespaces:

[XmlnsDefinitionAttribute("http://yournamespace/", "Your.Assembly.Namespace")]

You can include multiple of these attributes, typically in your AssemblyInfo.cs, allowing multiple code namespaces to be referenced by a single Uri namespace in Xaml.

This makes your namespace declarations more compact (since you can omit the assembly name). It also allows you some flexibility in reorganizing namespaces in the referenced assembly without breaking your markup.

EDIT: for example, if you point Reflector at the PresentationCore assembly, you can see attributes such as this at the assembly level:

[assembly: 
    XmlnsDefinition( "http://schemas.microsoft.com/netfx/2007/xaml/presentation"
                   , "System.Windows.Ink") ]

This is how the Uri import gets mapped to code namespaces.

like image 110
Dan Puzey Avatar answered Oct 22 '22 00:10

Dan Puzey