Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML clr-namespace - using incompatibility?

When sharing code between WinRT and WP8:

WP8 wants:

xmlns:vm="clr-namespace:MyApp.ViewModels" 

WinRT wants:

xmlns:vm="using:MyApp.ViewModels" 

This means you can't share XAML code - like user controls - between projects the way we could in WP7, Silverlight, WPF

Has anyone found a work-around ? XmlnsDefinition attribute looked like it might fix this, but MS took it out of WinRT.

like image 268
jlo Avatar asked May 06 '13 19:05

jlo


1 Answers

This issue, along with the limited workarounds, is explained in detail here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714088(v=vs.105).aspx

  • XAML on Windows Phone 8 and XAML on Windows 8 is not binary compatible. As stated earlier, the controls you use to build your UI on each platform are similar. They are similar in name, behavior, and the programming interfaces, or syntax, they expose. However, they are implemented specifically for each platform.
  • Namespace prefixes are different in XAML for Windows Phone 8 and XAML for Windows 8. This is illustrated by looking at the namespaces included in a basic page when you create it from scratch. [...] Comparing these namespace imports, you can see the subtle but significant difference in how a namespace is imported. In Windows Phone 8, imported namespaces are prefixed with clr-namespace:. In Windows 8 imported namespaces are prefixed with using:. This makes it difficult to use the same XAML, unless you can import namespaces using the same syntax.
  • XAML doesn’t support conditional compilation. As shown in Conditional compilation with preprocessor directives, conditional compilation is a useful technique for handling platform differences by compiling in a code path that targets a particular platform, and another code path when compiling for another platform. This makes it difficult to share a XAML page between both platforms, because you can’t address the previous issue by simply conditionally compiling in namespace imports that use clr-namespace for Windows Phone 8 and using: for Windows 8.

This shouldn’t be seen as a complete roadblock for sharing between Windows Phone 8 and Windows 8. The clear guidance is to design and build your UI separately for each platform, embracing the design guidelines for each. It is technically possible to circumvent these obstacles. You could create your UI during page initialization from code. You could load platform-specific XAML from resources at runtime and inject it as a string into the page. However, none of these techniques scale and they make the construction of your core asset—how your app looks to your user—a tedious and error-prone task. Your code sharing investment will give you a much larger return further down your app stack, by trying to share app logic, data models, viewmodels, etc.

[...] One technique for sharing UI that can be of use in some circumstances is to isolate parts of your UI into user controls and attempt to share those. [...] However, due to the limitations called out at the beginning of this discussion, the technique is limited to basic user controls. In addition to those limitations, you should consider the guideline to always build your user experience to suit the target platform. Sharing XAML controls is possible but limited. A good candidate for this kind of sharing is UI you want to display in a pop-up window or other widgets that you want to share because they typically will be composed of basic UI elements with no complex XAML and with simple styling.

like image 194
Peter Avatar answered Sep 19 '22 10:09

Peter