WinRT vs. Silverlight - Part 1 - XML Namespace

by Morten 15. September 2011 23:41

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

Tags:

Windows Runtime | WPF vs Silverlight | WPF | Silverlight

Comments (5) -

9/23/2011 1:24:55 PM

Joe White

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 White United States

9/23/2011 6:22:37 PM

Morten

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

Morten United States

9/30/2011 1:18:31 AM

Frenk

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

Frenk Switzerland

11/9/2011 7:44:36 AM

Richard

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 United Kingdom

11/12/2011 1:17:55 PM

Morten

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

Morten United States

Pingbacks and trackbacks (3)+

Comments are closed

About the author

Morten Nielsen

Silverlight MVP

Morten Nielsen
<--That's me
E-mail me Send mail

Twitter @dotMorten 

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2005-2011

Month List

RecentComments

Comment RSS