Prism is a well used framework in WPF and it’s also available in UWP. So let’s try it out. We’re going to start by looking at switching the default generated (blank) UWP application into one which uses Prism or more specifically Unity and Prism’s navigation functionality.
- Create a Blank UWP application
- From Nuget install the package Prism.Unity (I’m using v6.3.0)
- Edit App.xaml.cs and change the base class from Application to PrismUnityApplication (yes we’ll use Unity for this test), don’t forget to add using Prism.Unity.Windows;
- Remove all generated code from App.xaml.cs except for the constructor
- Open App.xaml and change it from using Application to PrismApplication, for example
<windows:PrismUnityApplication x:Class="MathsTest.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:MathsTest" xmlns:windows="using:Prism.Unity.Windows" RequestedTheme="Light"> </windows:PrismUnityApplication>
- Add the following bare bones method (yes it’s not handling possible startup states etc. as I wanted to reduce this code to the minimum)
protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args) { NavigationService.Navigate("Main", null); return Task.FromResult(true); }
- By default the UWP project will have created a MainPage.xaml but Prism’s navigation code expects this to be in a Views folder, so create a Views folder in your solution and move MainPage.xaml into this Views folder.
If you see an exception along the lines of “The page name does not have an associated type in namespace, Parameter name: pageToken” then you’ve probably not got a view with the correct name within the Views folder. Don’t forget to update the XAML and the xaml.cs files to include Views in the name space, i.e. TestApp.Views.MainPage
At this point we have UWP running from Prism.