Xamarin.Forms 2 and UWP
You have heard me talk about Xamarin.Forms before and you know that I’m all mobile… but what you might have missed is the fact that I’m a HUGE Windows Platform fan. This is why this new release of Xamarin has got me excited. In this release they included support for Universal Windows Platform Apps which, even though is still a preview version, it is a great tool for us as developers to start bringing our apps to even more devices.
For this post I’ll be using a very simple app that I created in the past when I wrote about going from console to mobile, however, this time is all about “from mobile to Windows Platform”.
We’ll be following the official documentation, so fasten your seatbelt and get ready to learn how is Xamarin.Forms wired and called within a platform specific project.
Feel free to download the initial source code from here, extract it somewhere and open the
Xevenshtein.sln file. Once the solution is open, upgrade all Xamarin.Forms packages to any version 2 (Xamarin.Forms 2.0 is part of Xamarin 4 release).
Then add a new project, this will be our UWP app and add the Xamarin.Forms NuGet package to it
Add a reference to the project where the forms application is, for us, such project is named
Xevenshtein and is a PCL
It is time to get our hands into the code: Open the
App.xaml.cs file that was created with the UWP app, look for this handler assignation
rootFrame.NavigationFailed += OnNavigationFailed;. Below this line we must initialize the Xamarin.Forms module (every platform specific project that uses Xamarin.Forms has to do this as one of the first things in the app lifecycle), to do so, simply call the
Then, we must get rid of the default UI inside the
MainPage.xaml, we won’t be using it since Forms has got us covered. Delete all code (it might be just a grid) within the
Then, we need to change our page type to a special kind of Xamarin.Forms page. First add this “attribute” inside the
I double-quoted attribute since the line above isn’t exactly an attribute, it is a namespace, somewhat like an
using directive but for xaml. Once we have added the namespace, replace the page
</forms:WindowsPage>, as you can see we’re using the
And that’s it for the XAML part, press
F7 to jump to the code-behind.
First things first, since our MainPage isn’t a
Page but a
WindowsPage, replace the inheritance
: Page for
Even though replacing the inheritance in this page isn’t needed (because the other partial definition of the class has it), I always do it, it gives me clarity of what type the page is.
Next… the big step: load your Forms page, this is done inside the
MainPage constructor and after the elements of the page have been initialized:
And that’s it, now you have a nice UWP app in a few steps.
For you: keep playing around with the Xevenshtein app (the code is on GitHub), or go ahead and create an UWP version your Forms apps: just imagine one of your apps running on an Xbox One or a giant Surface Hub… but remember that this isn’t the final release so if you find a bug, be a good Xamarin citizen and report it here. Oh, and don’t forget to take a look to other Xamarin 4 features, like xaml compilation or the general availability of Xamarin Insights.
For me: I’ll be using UWP to create a simple markdown viewer, I’ll post about it later on.