SharpGIS

#GIS from a .NET developer's perspective

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) -

  • Joe White

    9/23/2011 1:24:55 PM |

    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).

  • Morten

    9/23/2011 6:22:37 PM |

    Joe: So far I haven't been able to get that to work either. It might be a preview thing...

  • Frenk

    9/30/2011 1:18:31 AM |

    #if-def in XAML would be a *great* thing, let's hope they can squeeze it in the RTM!

  • Richard

    11/9/2011 7:44:36 AM |

    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.

  • Morten

    11/12/2011 1:17:55 PM |

    Richard: correct. You only need to declare one for all assemblies.

Pingbacks and trackbacks (3)+

Comments are closed