Share code with Add as Link

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

This topic describes how to share code between multiple projects using Add as Link available in Visual Studio, and how to leverage this technique when building an app to run on Windows Phone 8 and on Windows 8.

This topic contains the following sections.

In Visual Studio you can use the same file in multiple projects, without copying the file to each project. For example, in the following diagram, two projects share the same class, SharedClass.cs.

You can use this simple but powerful code sharing strategy to create a file or code asset once and then share it with multiple projects. Apart from being linked to your project, the file or acts like any other file in your project. When you edit the file, the changes are applied to all projects that link to the file.

You can use this code sharing technique when you are building your app to run on Windows Phone 8 and on Windows 8. Depending on the type of app you are creating, you’ll have some opportunity to share code this way across your projects. This is particularly useful when you’re trying to share code that isn’t portable, which is code can’t be shared inside a Portable Class Library. Windows Runtime APIs are not portable and can’t be used in a Portable Class Library. However, Windows Phone 8 and Windows 8 share a subset of Windows Runtime API, and you’ll probably want to try to write against this API once, and then share the logic in both apps. For more info about the Windows Runtime APIs that are common to Windows Phone and Windows 8, see Windows Phone Runtime API. The following diagram illustrates how you might share code by linking the file to multiple projects.

In this example, we have a Windows Phone 8 project and a Windows 8 project. The class SharedClass.cs contains code that isn’t portable, but is written using API common to Windows Phone 8 and Windows 8. We can share this class by adding it as a link to each project. By doing this, we’re writing code once and using it multiple times. We’ve abstracted the common, non-portable code into shared classes and each app project contains code and functionality that is specific to the app.

In general, you can use this technique for any code you’re able to isolate that’s platform-independent and used in both apps. Good candidates for this kind of sharing include, but are not limited to, the following scenarios.

  • App logic common to both apps, but not portable. In Windows Phone 8 and Windows 8, a lot of infrastructure code, or code that interacts with the operating system or external data sources, can be written using Windows Runtime API. Because Windows Phone 8 has adopted a subset of Windows Runtime, it’s possible to write code that uses this functionality once, and then share it across your apps. For example, both platforms have the same Windows Runtime API for networking, sensors, location, in-app purchase, and proximity. There’s significant overlap in these API on both platforms. This code can’t be placed in a Portable Class Library because it’s not portable. The .NET Framework portable libraries don’t support Windows Runtime. Instead, you can isolate the use of these APIs to classes in your app and share as linked code files in both your Windows Phone 8 and Windows Store app. If you’re using the free, Express versions of Visual Studio, you won’t be able to create Portable Class Libraries. In this case, you should use this code sharing technique to share your app logic. For example, apps that have been built using the Model-View-ViewModel (MVVM) pattern could share the model and viewmodel of you app.

  • User controls with no platform dependencies. Windows Phone 8 and Windows 8 both enable you to create rich user experiences using XAML. They implement this layer differently and have also defined the API in different namespaces. However, this divergence is not insurmountable. In fact, a lot of the functionality, types, and members are named the same between the platforms, and behave the same way. Although the overriding guidance is to build the best user experience as possible for each platform, there may be cases when you can extract elements of your UI that are common to both, write it once, and share with both apps. It isn’t possible to conditionally compile XAML, so the UI that’s defined in a shared user control must be platform independent. You can use conditional compilation in your code-behind class of the user control, so this offers some flexibility.

This is a simple way to write code once and share between your projects. For info about handling platform differences in your code, see Handling Windows Phone 8 and Windows 8 platform differences.

You link to a file from your project in Visual Studio.

  1. In Solution Explorer, right-click your project, and then select Add Existing item Or, you can type Shift+Alt+A.

  2. In the Add Existing Item dialog box, select the file you want to add, and in the Add drop-down list, click Add As Link.

  3. The file will be added your project as a linked file, which means it isn’t physically copied to the project, but instead just linked to it. A linked file will have a different icon than a file that was physically added to a project.

    • Shared

    • Not Shared

You can follow this procedure to link multiple files to your project. If you accidentally add a file to your project when you meant to add it as a link, make sure to delete the file from the project and from the project's folder on the disk before trying again to add it as a linked file.

See Also

Other Resources

Build 2012: How to Leverage your Code across Windows Phone 8 and Windows 8

Code Sample: PixPresenter

How to: Add Existing Items to a Project

Share functionality using Portable Class Libraries

Conditional compilation with preprocessor directives