Updating your Prism.Forms App to Prism.Forms 7+

Well, it’s been a while since Brian Laguna’s and the Prism team released Prism 7 and I’m finally getting around to updating some of my old projects. Prism 7 comes with some neat new features, but also carries some API changes, so if your existing Xamarin.Forms application is on an older version and you want to update to the latest, there will be some leg work involved. In this post, we’ll explore how to migrate from Prism.Forms 6.x to 7. Starting from scratch? Consider checking out my recently updated Bootstrap Series or the source code.

Updating Dependencies

Naturally, if you’re wanting to update your existing Prism.Forms application you’ll need to, well, update your packages! Depending on where you are in the development cycle it might not make sense to update everything wholesale, but if timelines re far off and your willing to break/fix a few things, now might be a good time to update your other project dependencies too, via NuGet

Simply select the individual packages you want to advance, or click the root packages node and click update to get the latest of everything. If you’re not updating everything, be sure to at least update immediate siblings to Prism.Forms, such as Prism.DryIoC.Forms if it isn’t update automatically.

Breaking Changes

Now that we’ve updated Prism (and perhaps everything else), things look…dicey.

Let’s set about fixing the broken bits of out bootstrap project – don’t worry! All the functionality is still, there, e just have some syntax to clean up and a few other housekeeping items to take care of. Let’s start with App.xaml.cs, making note of changes to RegisterTypes:

/// <summary>
/// Registers the types. Notice that we can set the name explicity by providing the
/// name parameter, or just use the nameof property for the page
/// </summary>
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// Register Navigation page
containerRegistry.RegisterForNavigation<Views.AppShellNavigationPage>("Navigation");
// Register Views
containerRegistry.RegisterForNavigation<Views.AppShell>("Root");
containerRegistry.RegisterForNavigation<Views.HomePage>();
containerRegistry.RegisterForNavigation<Views.SamplePage>();
containerRegistry.RegisterForNavigation<Views.SettingsPage>();
}

view raw
App.xaml.cs
hosted with ❤ by GitHub

If you are using the logger, as done in the bootstrap project, you’ll need to modify the reference slightly, like so:

/// <summary>
/// Logging provided by <see cref="Prism.Logging"/>
/// </summary>
/// <value>The logger</value>
public new ILoggerFacade Logger
{
// get { return base.Logger; } // Prism 6 – delet this
get { return this.Logger; } // Prism 7
}

view raw
App.xaml.cs
hosted with ❤ by GitHub

Also, if you followed my bootstrap project to the letter and implemented INavigationAware, we’ll need to remove the virtual declarations in favor of wither abstract or concrete declarations. Consider carefully how this may affect your app, depending on how you use INavigationAware.

/*
* Define Methods
*/
// Do something cool here when we naviagte away from the ViewModel
public abstract void OnNavigatedFrom(INavigationParameters parameters);
// Do something cool here when we have finished navigating to the ViewModel
public abstract void OnNavigatedTo(INavigationParameters parameters);
// Do something cool here when we start navigating to the ViewModel
public abstract void OnNavigatingTo(INavigationParameters parameters);

view raw
BaseViewModel.cs
hosted with ❤ by GitHub

Since I typically use INavigationAware to track page views, I opted for the abstract option to ensure deliberate operations on each page, but less rigorous options may be better suited for your scenario.

You can also go ahead and implement the (optional) constructors – unless you using platform specific dependencies (i.e – for example, if you wanted to expose ARKit functionality in your shared space… thats a blog post for another time though 🙃), this isn’t necessary but it’s nice to have if you need it down the way:

/// <summary>
/// Initializes a new instance of the <see cref="T:PrismForms.App"/> class.
/// </summary>
/// <remarks>
/// Used when no additional parmeters are needed
/// </remarks>
public App()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="T:PrismForms.App"/> class.
/// </summary>
/// <param name="platformInitializer">Platform initializer.</param>
/// <remarks>
/// Used when platfrom specific initializer is required – this will be used if you are using
/// very platform-focused libraries like ARKit that you want to resolve using DI
/// </remarks>
public App(IPlatformInitializer platformInitializer) : base(platformInitializer)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="T:PrismForms.App"/> class.
/// </summary>
/// <param name="platformInitializer">Platform initializer.</param>
/// <param name="setFormsDependencyResolver">If set to <c>true</c> set forms dependency resolver.</param>
public App(IPlatformInitializer platformInitializer, bool setFormsDependencyResolver) : base(platformInitializer, setFormsDependencyResolver)
{
}

view raw
App.xaml.cs
hosted with ❤ by GitHub

Summary

With that, our humble Xamarin.Forms app is now running on Prism 7! There are some more breaking changes to consider, so check out the official blog post if things still don’t compile and look for updates to my original blog post soon on the latest features of Prism 7 you’ll be itching to try!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s