James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Tags


Twitter


Guide to Add Windows Phone to Xamarin.Forms 1.3+ Projects

James MontemagnoJames Montemagno

I am a huge fan of Xamarin Studio and live inside of it around 50% of my development time. When I need to do library or Windows development though I head over to my good friend Visual Studio. Luckily the same exact project and solutions open in either IDE so it makes syncing with GitHub extremely easy. When I start up a Xamarin.Forms project I usually do it from Visual Studio so it spins up an iOS, Android, and Windows Phone Project. However, there are times that I am in XS, which doesn’t create the Windows Phone project so I need to add it after. Rob Gibbens wrote a great guide on adding Windows Phone projects to Xamarin.Forms in this case, but it is for the older Xamarin.Forms model pre 1.3. It is still a great guide and my guide follows this closely but updated with the new Application class requirements. Additionally, since Rob’s post we did release support for Windows Phone/Store RT apps and that process was documented as well, however Silverlight was left out, so let’s do it!

Going into this you will need at least Windows 8 and the Windows Phone SDK for Windows Phone 8.0 Silverlight.

Add Windows Phone Project

This is probably the easiest part of the process as all you need to do is right click on your Solution node and select Add -> New Project… From there you will need to find the Blank App (Windows Phone Silverlight) option under Installed -> Visual C# -> Store Apps -> Windows Phone Apps -> Blank App (Windows Phone Silverlight).

I personally name it: ProjectName.WinPhone and I turn OFF App Insights. You will then be prompted to select which version of Windows Phone to use. Simply select Windows Phone 8.0.

Restore NuGet Packages

You can always manually search for Xamarin.Forms in the NuGet Package manager, however it is easiest to actually right click on the SOLUTION node and select Manage NuGet Packages for Solution… This will allow you to see ALL NuGet packages installed in your projects and add them into your Windows Phone project. Simply Find Xamarin.Forms in the list, select Manage, and check ProjectName.WinPhone.

You may have additional NuGets installed so be sure to add those to your Windows Phone Project as well.

Fix Toolkit.Content

Xamarin.Forms uses the all powerful WPToolkit for some of its controls. When we added the Xamarin.Forms NuGet it also installed the WPToolkit NuGet and some new images were added under Toolkit.Content. We must select all of the files in this folder and mark the Build Action as Content and the Copy to Output directory to Copy always.

Add Portable Class Library or Shared Project Reference

Of course now we need to add in our shared code. Simply right click on the Windows Phone project’s references and find your “ProjectName” portable class library or Shared Project.

Modify MainPage.xaml

Xamarin.Forms 1.3 changed the normal flow of Xamarin.Forms applications include the Windows Phone’s main page, which is now inherits from FormsApplicationPage. We will now add in a custom xmlns of forms and then inherit from FormsApplicationPage:

Ensure you change “ProjectName” with your actual project name.

Modify MainPage.xaml.cs

Of course there is a bit of code behind to modify in the MainPage.xaml.cs to reference this new FormsApplicationPage and also call the LoadApplication of Xamarin.Forms. Simply replace the entire class with the following:

Ensure you change “ProjectName” with your actual project name.

You should be all set at this point with your Windows Phone project ready to go! 

Live, Love, Bike, and Code

Comments