Prism > 7.0 Recap

Almost two years ago I wrote an article about how to update your project to Prism 7.0. Since then there have been a lot of changes introduced in Prism, so in this article, I’ll be doing a recap of versions 7.1 and 7.2.

Version 7.1

Using INavigationParameters instead of NavigationParameters

If you updated your project from 7.0 to a more recent version you probably got an issue related to the parameter type you receive on the Navigation interface methods which is no longer NavigationParameters.

You have to use INavigationParameters instead.

——————————————————————————————————————–

XAML Navigation

Since version 7.1 is possible to navigate from XAML (Without using any view model). To do it you just have to add the prism reference in your XAML.

xmlns:prism="clr-namespace:Prism.Navigation.Xaml;assembly=Prism.Forms"

Then on any Command property specify the page name you want to navigate to.

Command="{prism:NavigateTo MainPage}"

——————————————————————————————————————–U

Inject services on ValueConverters using ContainerProviders

“The Container Provider now allows types like ValueConverters to include Dependency Injection of Types/Services in the ctor, and allows the converter to be declared in XAML”.

So, in case we have a value converter and we want to change the color of an element according to the device idiom (Tablet or Phone).

Since prism has a pre-registered service to know the device info called IDeviceService we will inject it in the converter.

And to use it in XAML we use the ContainerProvider:

Version 7.2

AutoRegisterForNavigation

Did you remember the time when you have to register all pages you use in your Prism application?

So now you have the possibility of not doing that by using [AutoRegisterForNavigation] property instead. Adding it to your App.xaml.cs file will register any Page and any ViewModel associated with that page based on naming convention PageName + ViewModel, ex: If my page is called LoginPage the view model should be called LoginPageViewModel.

——————————————————————————————————————–

Sending Parameters using NavigationParameters

To send parameters to other ViewModel, normally we create a new instance of NavigationParameters.

Now it supports passing parameters this way:

——————————————————————————————————————–

Receiving parameters using INavigatingAware deprecated

If you were using INavigatingAware to receive parameters you will have to change it to IInitialize.

So instead of:

Now you will have:

There is also an async version you can use: IInitializeAsync.

——————————————————————————————————————–

Showing popups using the new IDialogService

With this pre-registered IDialogService now it’s possible to show popups. To use it you just need to implement the IDialogAware interface

For more information, you can see this previous article about it.

——————————————————————————————————————–

Using IAutoInitialize

The IAutoInitialize is used to initialize a property with the same parameter name you are passing to the ViewModel you are navigating to by doing an automatic mapping.

For example, if you want to send a parameter called Message to the MainPageViewModel. Normally what we do is sending the parameter when navigating to the ViewModel and then receive it on the OnNavigatedTo method.

Now if we implement the IAutoInitialize interface in the ViewModel we will receive the parameter by doing an automatic mapping with the name of the property.

If you associate the attribute AutoInitialize(true) to a property then if you don’t pass that parameter it won’t navigate.

Check the full source code here.

Happy coding!

You may also like

5 Comments

  1. Thumbs up! Hugely informative blog. Got some helpful pointers which I will apply in real-life situations. Thanks a lot for uploading this one. If you are Searching for

  2. Hi, thanks for your blog, it’s very interesting.

    Do you know if it’s possible (and It’s useful) use Prism with Shell?
    For example, for me it’s importante to be able send parameters directly to my ViewModel like in the OnNavigatedTo event.