In today’s article, I will discuss some of the essential improvements of Prism 7 for Xamarin as well as the minimum steps to get you up and running using Prism 7 in Xamarin forms, so let’s get started.
.NET Standard Support
The long awaited .NET support for prism has arrived. It was during the 6.3 version days that I was having some problems with PCL projects wherein the more profiles that you select the less .NET APIs you can use but nevertheless .NET Standard will save the day.
Changes in Registering and Resolving of Types
In the version 7 of Prism, major changes were applied for Registration and Resolution of types so that it will not be difficult to change from one container to another. Interaction with the container is now completely abstracted through IContainerRegistry and IContainerProvider. In the previous version of Prism, you are directly working with Container as shown in Figure 1.
In prism Version 7, you will use IContainerProvider to manually resolve your services as shown in Figure 2.
Signature change of RegisterTypes method
The changes related to RegisterTypes signature is related to the IContainerRegistry which is currently being passed as a parameter in order for you to register your services and page navigations while in the previous version you need to use the Container to accomplish the registrations.
Figure 3 shows the old method signature of RegisterTypes.
Figure 4 shows the new changes in the method signature of RegisterTypes in Prism 7
Changes in Tabbed Navigation
Another improvement in Prism 7 is the navigation in Tabbed pages. Previously, if you have something like this in Figure 5 it will traverse Page 1 up to Page 2.
In Prism 7, when you execute the code above it will still traverse Page1 up to Page 2 but will treat it as separate pages. If you want to navigate to a specific tab like for example Page 2, you need to use the “selectedTab” keyword as shown in Figure 6.
Keep in mind also that execution of INavigationAware changes:
- OnNavigatingTo will be executed on TabbedPage and All children Tabs.
- OnNavigatedTo will be executed on TabbedPage and Selected Tab Page.
- OnNavigatedFrom will be executed on TabbedPage and Selected Tab Page
Dynamic Tab Generation
We can now create new Tabs dynamically in Prism 7 by using the keyword “createTab” as shown in Figure 7.
If you want to wrap your newly created Tab with navigation page you can add “|” in createTab as shown in Figure 8.
Prism 7 has support for GoBackToRoot which is the equivalent of PopToRoot of Xamarin Forms as shown in Figure 9.
One nice feature of Prism is that there is already a template that we can use because it already sets some items like namespaces for the Prism and AutoWireViewModel locator for our View and ViewModel binding. Let’s visit the Visual Studio Market Place then search for the Prism 6 template pack as shown below:
Creating a New Xamarin Forms Using Prism 7
Now that we have discussed about the changes in Prism 7 let’s try to integrate Prism 7 with our Xamarin Project. Let’s create a new Xamarin forms with .Net Standard support as shown in Figure 10.
After creating the project lets try to download the Prism Library through Nuget Package Manager. Search for the Prism Nuget and select the desired IOC for your Xamarin Project. Select the three target projects where the Prism library will be installed as shown in Figure 11.
Configuring the App Class
If you are familiar or have been working with the previous versions of Prism you just need to inherit from the PrismApplication and change the Signature of the App Constructor. You also need to implement the OnInitialized and the newly changed signature for RegisterTypes as shown in Figure 12.
You also need to change the XAML side of the App. From the normal Application Tag to PrismApplication tag. Just remember to add the Namespace of the prism library as shown in Figure 13.
Don’t forget to register your navigation pages like from the previous Prism versions under RegisterTypes method and in your OnInitialized Method point it to your starting page. Try to run your app and it should be working fine.
To wrap things up, we discussed some of the cool and awesome improvements of Prism 7 like Dynamic Tab Generations, Unified container namespace, navigations improvement and most of all support for .Net Standard. To learn more of the details for all changes of Prism 7 kindly visit this link:
And if you like, you can support Prism development by becoming a Patron by visiting this link:
Once again a big thanks to Bryan Lagunas, Dan Seigel and all Prism contributors for a job well done.
Happy Coding 🙂