Getting Started with Xamarin.Forms 2.0’s Windows 10 Preview

Avatar

James

With the release of Xamarin 4 came Xamarin.Forms 2.0, introducing new features and optimizations to help you build great native cross-platform mobile apps. In addition to ListView caching strategies, pinch gestures, and new properties came the first public preview of Xamarin.Forms for Windows 10 (UWP) applications. While Xamarin.Forms already supports Windows Phone and Store targets, Windows 10 introduces an Universal Windows project that can target both desktop and phone platforms, allowing you to easily target even more devices.

promo

Getting Started

Before you begin creating Universal Windows 10 apps with Xamarin.Forms, there are a few prerequisites that need to be installed.

Once you have everything installed, it is time to get your Xamarin.Forms project ready. Xamarin.Forms 2.0 not only adds preview support for Windows 10 apps, but also add a plethora of additional features, optimizations, and awesome for your mobile apps. To get started, head over to the NuGet Package Manager in Visual Studio.

Manage NuGet Packages

Select the “Upgrade available” filter and update all of your NuGets to Xamarin.Forms 2.0.

UpgradeNuGets

Note: Visual Studio may prompt to be restarted after this step.

Adding the Universal Windows App

Once all your NuGets are up to date, it is time to add a Blank Universal Windows project to your Xamarin.Forms solution. This can be found under Add New Project -> Windows -> Universal -> Blank App.

UWP Project Template

Add NuGets & PCL/Shared Project Reference

When you create a new Xamarin.Forms project, all of the references and NuGet packages are automatically added for you. However, for existing Xamarin.Forms projects that want to target UWP as well, you must add them in manually. First, add the Xamarin.Forms project. Right click on the solution and find “Manage NuGet Packages…”, and from the filter dropdown select “Installed”. Find Xamarin.Forms, and check the UWP project to install the same version of Xamarin.Forms into your UWP app.

Add Forms to UWP

Now, add your shared code that contains your Models, Views, and ViewModels. Inside the UWP application right-click “References”, and select “Add Reference”. Under projects, you will will find all of the projects in your solution. Find your PCL project or shared project, check it, and click OK.

Reference Manager

Update App.xaml.cs

All Windows apps have an App.xaml for application-level configuration. Just like a Xamarin.Forms XAML page, this page also has a C# code behind file for lifecycle events. Open it up and you will find an OnLaunched method.

Find the following line of code:

and add the Xamarin.Forms Init call:

Update MainPage.xaml

Open up MainPage.xaml of the Windows 10 app and remove the default “Grid” that was created for you so that the page is now completely empty. Next, add a new namespace in the XAML and change the Page to be a Xamarin.Forms Page:

Update MainPage.xaml.cs

Update the code behind file to initialize your Xamarin.Forms app:

Replace YOUR_NAMESPACE with the namespace that is found in your Xamarin.Forms Application class, which is usually located in your shared code under App.cs.

Configure App to Deploy

Sometimes Visual Studio may not set new project to build and compile by default. To ensure that it does, right-click the solution and open up the Configuration Manager. Check the UWP app to build and deploy, and you are all set!

Configuration Manager

Add images assets and any other NuGets

The last step is to simply add any image assets or add back in any NuGet or Plugins for Xamarin that you may have as a dependency. Be sure that any of these dependencies have also add Windows 10 (UWP) support for compatibility.

Learn more and provide feedback

To learn more about all of the new features in Xamarin.Forms 2.0 be sure to read through the release notes. You can check out the full source code for the weather sample application shown above on my GitHub. Windows 10 (UWP) support is in preview, so there is a possibility that you may run into some rough edges. To help make Xamarin.Forms for UWP amazing, please be sure to leave feedback by filing an issue if you experience one.

Avatar
James Montemagno

Principal Program Manager, Mobile Developers Tools

Follow James   

0 comments

    Leave a comment