WinRT vs. Silverlight - Part 1 - XML Namespace

See intro blogpost here.

For the most part your XAML ports right over to WinRT. However an important change is how you register namespaces from your assemblies.

Here's how you do this in Silverlight and WPF for an assembly "MyAssembly.dll" and namespace MyAssembly.MyNamespace:

    xmlns:local="clr-namespace:MyAssembly.MyNamespace;assembly:MyAssembly"

And similar for namespaces in the same assembly as where the XAML file lives (ie MyAssembly):

    xmlns:local="clr-namespace:MyAssembly.MyNamespace"

In WinRT, you only declare the namespace (never the assembly) and instead use "using" instead of "clr-namespace":

    xmlns:local="using:MyAssembly.MyNamespace"

Unfortunately we don't have #if-def statements in XAML so you can just use compiler directives on your XAML and make it work on both platforms. So until we get that (or Microsoft reverts the above change) you are going to have to duplicate and maintain two sets of XAML. :-(

I actually do like this change, and this is probably how it should always have been, but it's a change that cause a lot of pain for developers trying to reuse their existing codebase. The benefit doesn't seem to pain (from what I understand the Windows team simply didn't like it said "clr" in there, plus they don't have the exact same concepts down in the runtime so the assembly part was left out.)

Comments (5) -

  • Does WinRT support the XmlnsDefinitionAttribute to map your CLR namespaces to your own XML namespaces? If so, that could be used as a way to work around this and use the same XAML file across both platforms (in the cases where that's worthwhile -- obviously some things would need to be redesigned for touch).
  • Joe: So far I haven't been able to get that to work either. It might be a preview thing...
  • #if-def in XAML would be a *great* thing, let's hope they can squeeze it in the RTM!
  • If you're not declaring the assembly, does that mean you only need one xmlns for a cross-assembly namespace? It gets quite tedious having to have one for "System,mscorlib", one for "System,System", etc.
  • Richard: correct. You only need to declare one for all assemblies.

Pingbacks and trackbacks (3)+

Add comment