Tuesday, 29 December 2015

Prism for Xamarin.Forms 6.2

In early December, Brian Lagunas announced a pre-release of Prism for Xamarin.Forms 6.2.0. Over Christmas I checked it out and here’s what I found out.

The big changes are around bootstrapping and navigation, with simplification and new features being the order of the day. There are also some breaking changes, in that INavigationPageProvider and NavigationPageProviderAttribute have both been removed as they are no longer needed due to the INavigationService handling the majority of navigation scenarios now.

Bootstrapping

The CreateMainPage and InitializeShell methods are now deprecated, with the OnInitialized override now being used to navigate to the initial page of the app:

1 public class Bootstrapper : UnityBootstrapper 2 { 3 protected override void RegisterTypes() 4 { 5 Container.RegisterTypeForNavigation<HomePage>(); 6 } 7 8 protected override void OnInitialized() 9 { 10 NavigationService.Navigate("HomePage"); 11 } 12 }

The page being navigated to is registered in the RegisterTypes override, with navigation then being invoked from the OnInitialized override. As well as being a simpler app bootstrapping process, it also removes the problems with some navigation scenarios on startup, such as navigating to a MasterDetailPage and then changing the Detail page,.

Navigation

The INavigationService now supports a number of new features, including passing parameters via the GoBack method, support for URI-based navigation, and deep linking. For information about the new navigation features see Brian’s blog post.

The INavigationService now detects the type type of page being navigated from, and handles the navigation for you. For example, it will detect that navigation is originating from a MasterDetailPage and will set the MasterDetailPage.Detail property to the navigation target, instead of pushing a new page onto the navigation stack.

In addition, the INavigationService supports more complex navigation scenarios. For example, imagine that you want your app to navigate to a MasterDetailPage that sets the Detail page to a NavigationPage, and then displays a ContentPage. As with all Prism-based apps, the pages must first be registered with the navigation service:

1 protected override void RegisterTypes() 2 { 3 Container.RegisterTypeForNavigation<HomePage>(); 4 Container.RegisterTypeForNavigation<MyNavigationPage>(); 5 Container.RegisterTypeForNavigation<MyMasterDetailPage>(); 6 Container.RegisterTypeForNavigation<WeekPage>(); 7 Container.RegisterTypeForNavigation<MonthPage>(); 8 Container.RegisterTypeForNavigation<YearPage>(); 9 }

After registering the pages, it then becomes possible to perform the required navigation in one call:

1 NavigationService.Navigate("MyMasterDetailPage/MyNavigationPage/MonthPage");

The result of the navigation will show a MasterDetailPage whose Detail page is a NavigationPage, with the MonthPage being pushed onto the navigation stack for display. In addition, the navigation stack will be as expected, and so backwards navigation will return down the stack.

For me this is the killer feature in this release of Prism for Xamarin.Forms, as when combined with the URI-based navigation support and parameter passing, it becomes possible to launch a Xamarin.Forms app from a website and deep link to content within the app, while maintaining the correct navigation stack.

Summary

The original release of Prism for Xamarin.Forms was a welcome addition to the Prism family, and simplified the development of Xamarin.Forms apps. However, it was slightly restrictive in some areas, but with the preview release of Prism for Xamarin.Forms 6.2.0 the majority of these restrictions no longer exist.

While not everyone is a believer in using such libraries in mobile app development, Prism for Xamarin.Forms offers a clear productivity boost and simplifies the development of fully unit testable Xamarin.Forms apps that have a clean separation of concerns. Going forward I’ll certainly be using it for my own app development efforts, and I look forward to it moving from a pre-release to a stable release.

2 comments:

  1. How do you provide external information to the OnInitialized method to decide where to deep-link to?

    ReplyDelete
    Replies
    1. This is where you need the deep linking functionality provided by Forms 2.3 (currently only a pre-release). You can use the AppLinkEntry type to create a deep link, and when the user taps the deep link in their search results, the App.OnAppLinkRequestReceived override will fire. You can then process the deep link and take action with it as required (e.g. pass it's data to OnInitialized, or call the NavigationService directly from App.OnAppLinkRequestReceived).

      Delete