Adding a Universal Windows Platform (UWP) App

PDF for offline use

Let us know how you feel about this


0/250

Before you start, create (or use an existing) Xamarin.Forms project. You should be running Visual Studio 2015 on Windows 10 to build UWP apps. You can read more about the Universal Windows Platform on Microsoft's website.

UWP is available in Xamarin.Forms 2.1 and later, and Xamarin.Forms.Maps is supported in Xamarin.Forms 2.2 and later.

Check the known issues and troubleshooting sections for helpful tips.

Follow these instructions to add a UWP app that will run on Windows 10 phones, tablets, and desktops:

1 . right-click on solution > Add > New Project... and add a Blank App (Universal Windows):

2 . right-click on project > Manage NuGet Packages... and add the Xamarin.Forms 2.0 package. Ensure the other projects in the solution are also updated to Xamarin.Forms 2.0.

3 . Make sure the new UWP project is built in the Build > Configuration Manager window (this probably won't have happened by default). Tick the Build and Deploy boxes for the Universal project:

4 . right-click on project > Add > Reference and create a project reference to the Xamarin.Forms application project (PCL or Shared Project).

5 . Edit App.xaml.cs to include the Init method call inside the OnLaunched method around line 63

// under this line
rootFrame.NavigationFailed += OnNavigationFailed;
// add this line
Xamarin.Forms.Forms.Init (e); // requires the `e` parameter

6 . Start editing MainPage.xaml by removing all the contents between the <Page> elements (probably just an empty Grid element).

7 . Then in MainPage.xaml - add a new xmlns entry for Xamarin.Forms.Platform.UWP:

xmlns:forms="using:Xamarin.Forms.Platform.UWP"

8 . Still in MainPage.xaml - change the root element <Page to <forms:WindowsPage:

<forms:WindowsPage
...
   xmlns:forms="using:Xamarin.Forms.Platform.UWP"
...
</forms:WindowsPage>

9 . Edit MainPage.xaml.cs to remove the : Page inheritance specifier for the class name (since it will inherit from WindowsPage due to the change made in the previous step):

public sealed partial class MainPage  // REMOVE ": Page"

10 . Still in MainPage.xaml.cs, add the LoadApplication call in the MainPage constructor (around line 28) to start your Xamarin.Forms app:

// below this existing line
this.InitializeComponent();
// add this line
LoadApplication(new YOUR_NAMESPACE.App());

11 . Finally, add any local resources (eg. image files) from the existing platform projects that are required.

Known Issues

  • The look of some views/pages is not yet finalized
  • There are a couple known crashers around navigation
  • Text alignment may not be perfect in some titles

Leave feedback in the forum or post bugs here.

Troubleshooting

"Target Invocation Exception" when using "Compile with .NET Native tool chain"

If your UWP app is referencing multiple assemblies (for example third party control libraries, or your application itself is split into multiple PCLs), Xamarin.Forms may be unable to load objects from those assemblies (such as custom renderers).

This might occur when using the Compile with .NET Native tool chain which is an option for UWP apps in the Properties > Build > General window for the project.

You can fix this by using a UWP-specific overload of the Forms.Init call in App.xaml.cs as shown in the code below (you should replace ClassInOtherAssembly with an actual class your code references):

// you'll need to add `using System.Reflection;`
List<Assembly> assembliesToInclude = new List<Assembly>();

//Now, add in all the assemblies your app uses
assembliesToInclude.Add(typeof (ClassInOtherAssembly).GetTypeInfo().Assembly);

//Also do this for all your other 3rd party libraries

Xamarin.Forms.Forms.Init(e, assembliesToInclude);
// replaces Xamarin.Forms.Forms.Init(e);

Add a reference to each assembly that is referenced by the app.

Dependency Services and .NET Native Compilation

Release builds using .NET Native compilation can fail to resolve dependency services that are defined outside the main app executable (such as in a separate project or library).

Use the DependencyService.Register<T>() method to manually register dependency service classes. Based on the example above, add the register method like this:

Xamarin.Forms.Forms.Init(e, assembliesToInclude);
Xamarin.Forms.DependencyService.Register<ClassInOtherAssembly>(); // add this

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.