James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Live, Love, Bike, and Code

Share


Tags


Shared Code Projects in VS: Windows Phone, Windows Store, and Xamarin!

At Build, Microsoft announced Shared Code Projects to simplify how developers are sharing code between platforms. We have had official portable class library support for Xamarin apps since late last year and it has been wonderful, however once you mix WP8 into the mix the PCL profiles get a bit limited. Shared Code Projects attempt to fix this by allowing you to create a project that are a shell for code that can be added into any project. It is basically glorified file linking with a nice UI to help you debug your code better per project. You will still have to do a bunch of #ifdefs though if you are trying to use functionality that is not available in a specific platform.

Shared Code != Universal App && Shared Code != PCL

So it should be stated that Shared Code projects are not universal apps and they are not PCLs. A Universal App is just a project template for WP8.1 and WinStore 8.1 apps that use a shared code project. The idea of a Universal App is simply that these two platforms have a shared project and maybe they are able to share some of the UI which is XAML (I still have to explore this more). Also, a Shared Code project differs from a PCL because the PCL does not have the ability to call platform specific code without an abstraction layer like the shared code project can. Additionally, they work completely different at Compile vs Deploy time. Remember that PCL’s can compile against a “contract” and implementation, but at build and deploy time the platform specific version will is used. The shared code project is compiling and deploying against what is in each parent project and their references.

So to me this means that a PCL is for library creators and a Shared Code project is for app creators, but an app creator can also live completely in PCL land if they want. I have been and I enjoy it, but I am still constrained to what PCL limits me to, but allows me to easily add platforms in the future without modifying code. So I think I will start more projects with shared code projects in the future.

Get it in VS!


While Miguel tweeted that support for shared code projects will be added to XS in the future if you live 100% of the time in VS then you can get started today with shared code projects. The first thing you need to do is install the shared code project visual studio plugin assuming you already have Visual Studio 2013 Update 2 RC installed.

With that installed we can go ahead and create a new project:

Then you can add in your iOS and Android apps like normal. Once you want to start writing code in your Shared Code Project you will want to add them as a reference to your other projects so you can get intellisense and bring in namespaces.


Once I have added them here you will be able to see the reference in the project references:

What am I coding against?

Shared code project actually have no references… well actually they have every reference of the parent. What is really cool is that you can toggle between each project when you have a file open from the shared code project.

This means you will probably have to do a lot of toggling between everything to make sure it compiles correctly, but it is pretty slick. So now if I wanted to add HttpClient into the mix here I would add the reference to my iOS and Android project like I normally would, and then start writing code in my Shared Code project:

And there you have it, Shared Code projects integrated into Visual Studio for Xamarin projects!

Copyright © James Montemagno 2014 All rights reserved. Privacy Policy

View Comments