Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Performing code reuse in Windows Phone

From Wiki
Jump to: navigation, search

Today there are multiple platforms in the Windows universe that you might want to service with your apps. Keeping versions for Windows Phone 7, Windows Phone 8 and Windows 8 is a common scenario. Developing multiple versions of an app however should not multiply work. Here are some tips how to avoid duplicate work and keep the app maintainable.

Article Metadata
Created: influencer (10 Feb 2014)
Last edited: influencer (01 Mar 2014)



A typical Windows Phone solution consists of code, resources (resx), xaml, styles and other assets like images or (static) data files. Being able to share the biggest part of it can significantly reduce your workload and improve maintainability. There are different ways to reuse code and the other items in a solution. Here we present an overview with links to background articles.

Elements that can be shared - and that can't

In a recent, featured discussion on the ND Discussion Board (Thread: Performing code reuse in Windows Phone 8) it's postulated that some elements can be shared and some can't be shared. As we'll see later the list of sharable elements is long and with good ideas problems might be overcome. The example for this are the resources that can be shared if the differences for the platforms are small and the resource namespaces can be kept equal.

If the readers have solutions for other problematic elements the community would gratefully honor an article describing the solution.


The most important preparation is to divide code from the UI. You might object now that the division of the XAML UI from the code-behind in C# is enough but in order to create different UIs for the platforms the code should not be tied so strongly to the UI. One solution here is the MVVM pattern Understanding the Model-View-ViewModel Pattern. What you can gain additionally is so-called 'Blendability', i.e. a rich design experience in Microsoft Blend. There are lots of articles, videos/webcasts on MVVM and several toolkits proposing to simplify it's usage.

Ways to reuse code

There are different approaches to sharing the elements of a solution. In MSDN there's a very good overview of some of the techniques that can be used to share code and XAML, Maximize code reuse between Windows Phone 8 and Windows 8. Sharing resources is not covered there.

The following list shows the techniques grouped by type of solution element:

Multiple types (works for code, resources, images. assets...)


With code there are many options to share complete assemblies, complete source files or parts of them.

  • Putting code in a separate assembly and referencing it - The ideal occurrence are Portable Class Libraries (PCL, see below). If not using PCL it's difficult to use managed code because of differencies in the .net framework base libraries. Unmanaged code, i.e. DLLs written in C/C++, should be simple to share across platforms.
  • Using preprocessor directives - Preprocessor directive are used in conjunction with Copy/Paste (arghh) and 'Add as Link' to circumvent small differences in code. They are usable in C++ and C# code but not in XAML. MSDN has a nice article.


Resources present a special challenge, because there are no simple ways to conditionally switch parts of resource files. If the greater parts of your resources (strings) are identical, you can use the following techniques:


XAML files pose comparable problems as resources. However, some parts of them can be put into static resources. You can use static resources for storing strings or data. The most important problems here are different namespaces in Windows and Windows Phone and differences in the controls themselves. Due to the screen size constraints the XAML UIs should be developed individually anyway.(TODO)

Limiting Factors

Beginning with Visual Studio 2013, support for Windows Phone 7 is dropped. So upgrading an elder solution poses problems:


Reusing code or other solution assets can save a lot of time and improve the stability of multi-platform projects. The details however can be a little tricky for some kinds of solution items.

This page was last modified on 1 March 2014, at 00:16.
664 page views in the last 30 days.