Multi-Targeting: Sharing XAML Code between platforms / Programmatically loading resources

by 4. February 2011 22:05

Below I show the output from both the Desktop (top) and Silverlight (bottom) applications for the open source Password Manager project  (changeset 84433); I am now sharing the XAML code as well as all source code for the Security Module. 

Note: the password manager project has a "single" codebase that is shared between the Windows Phone, Desktop and Silverlight applications. 

 320 Note in the left image how all of our Views are linked files - these are linked to the Silverlight project.  All of the other files are linked to the Phone project.   The only code for this project that is not linked is the resource files (see arrow); the resources will manage differences between platforms. 

Since our linked projects have the same namespace, it makes it relatively easy to share the same user control 

Where it became challenging was sharing XAML that required resources and used a baseclass that resided in a separate project, e.g., the following XML file can't be shared because it has a reference to Silverlight assembly (line 6 top pane) so that it can use the UserControlBase in the Gwn.Infrastructure.xxxx project.

The solution was to create a UserControlBase in the "current" project and have it derive from the UserControlBase - reference middle pane.

With that done I was able to make the necessary reference (line 6 in the bottom pane of the image above) in a manner that will allow me to link (share) this XAML with the Desktop application. 

The Gwn.Infratructure.Base.UserControlBase class has conditional compiler statements that will pull in the applicable Resource file for the platform being executed.  This proves very useful because with a single declaration all of our UserControls (derived from UserControlBase) will have access to all of the resources, i.e., Styles, Templates, Converters, etc.

Below (top pane) we show the _Shared.Generic.xaml file which loads our other resources.   Each of the resources will reference the Common.Generic.xaml which will have the global colors used throughout the system for the various styles and templates. 

Likewise it serves as a great convenience to have all of the converters available, while preventing the need to reference these external resources in XAML.   We've reduced the declaration requirements in each of our XAML files while providing them all of the resources available.

 Source code available at: 


Blog videos and references to CodePlex projects are no longer valid