When I saw the new Universal Windows App project type demonstrated in the Microsoft Build keynote last week, I just had to try it out with an existing Xamarin project. This actually proved quite simple; just copying one line from the Windows project file to the xamarin projects did the trick:

(note that the standard code sharing features of the Universal Windows App project template, across Windows Phone and Store, are quite powerfulf as well; see here for a good overview)



UPDATE 22-4-2014 Add Shared Project Reference… item on the context menu for the References node for your (Xamarin) projects. Instead of manually modifying the Xamarin project files as described in steps 9 and 10 below, you can also install the Shared Project Reference Manager Visual Studio extension. This gives you anitem on the context menu for thenode for your (Xamarin) projects.

So here is how you can create a Universal App solution for Windows Store, Windows Phone, Xamarin Android and Xamarin iOS in Visual Studio 2013 Update 2:

Install Visual Studio 2013 Update 2 Create a new Blank App (Universal Apps) for Windows and Windows Phone; let’s name it QC2Test.

In the VS Solution Explorer, add a new Xamarin iOS Universal project to the QC2Test solution folder

Name: QC2Test.iOS

Folder: create it in the same QC2Test subfolder that contains the Windows projects.

A dialog can appear now, about a conflict in the new project file – select Discard to keep the file system based changes.

Fill in the iOS project properties as usual; typically the iOS Application tab.

In the VS Solution Explorer, add a new Xamarin Android project to the QC2Test solution folder

Name: QC2Test.Android

Folder: create it in the same QC2Test subfolder that contains the Windows projects.

Fill in the Android project properties as usual; typically Application – API levels; also set the Conditional Compilation Symbol __ANDROID__ for all project configurations.



Place the Shared Project App.xaml.cs within #if NETFX_CORE … #endif so it won’t cause errors in Xamarin projects.

Unload the Windows project, edit the project file, and copy this line to the clipboard:<Import Project=”..\QC2Test.Shared\QC2Test.Shared.projitems” Label=”Shared” /> Now reload the project. Unload the Android and iOS projects, edit them, and paste the above line directly above the first existing Import element.

Now reload the projects. Now all projects will show Shared references (which are actually includes):

To test code sharing, add a class SomeSharedClass.cs to the shared project and build the solution; in class view, you can now see the class in all 4 platforms:



Benefits

Until now, I used a code sharing approach with a separate solution file and a separate shared project file for each platform in a shared solution folder and shared project folders, as described here. In comparison, this new Universal App sharing model has these benefits:

A LOT less manual steps to setup the solution structure.

Only one shared project needed instead of one for each platform – no more keeping project item add/rename/delete in sync.

NuGet packages are on the head projects only, no longer on the shared project. This prevents overwriting of the platform-specific packages.config file that occurred when using multiple project files in the same project folder.

AssemblyInfo.cs is on the head projects only; we no longer need #ifdefs for platform specific assembly attributes on the shared project.

Limitations

Note that you cannot build this solution with Xamarin Studio, since it does not understand the shared project include.

However, you can open the iOS project file directly in Xamarin Studio on a Mac to add Storyboards, Xibs etc and you can edit them in XCode just fine.

Conclusion

The new sharing model of the Universal Windows Apps project type also works well for Xamarin iOS and Android projects. This looks like the way forward for sharing code across platforms in those cases where you want to avoid the limitations and overhead that a Portable Class Library brings. Currently you will lose the ability to compile from Xamarin studio, but Xamarin is expected to add support for this project type soon.

NJoy!