Xamarin: Getting Started With Prism 7

XamarinWithPrism.jpg
Image credit to OraculoTI

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

NetStandard.png
Image credit to MSDN

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.

ManualResolve.PNG
Figure 1: Old method for resolving services.

In prism Version 7, you will use IContainerProvider to manually resolve your services as shown in Figure 2.

NewResolve
Figure 2: New method for resolving services.

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.

RegisterTypes.PNG
Figure 3: Old Signature of RegisterTypes

Figure 4 shows the new changes in the method signature of RegisterTypes in Prism 7

NewRegisterTypes.PNG
Figure 4: New Signature of RegisterTypes

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.

TabbedNavigation.PNG
Figure 5. Sample Tabbed Navigation

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.

ImprovedTabbedNavigation.PNG
Figure 6. Improved Tabbed Navigation

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.

DynamicTab.PNG
Figure 7. Dynamic Tab generation

If you want to wrap your newly created Tab with navigation page you can add “|” in createTab as shown in Figure 8.

DynamicCreation
Figure 8. Dynamic Tab generation with Navigation Page

GoBackToRoot Support

Prism 7 has support for GoBackToRoot which is the equivalent of PopToRoot of Xamarin Forms as shown in Figure 9.

GoBackToRoot
Figure 9. GoBackToRoot Support

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.

Create1.PNG
Figure 10. Creating .net standard Xamarin project

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.

PrismUnity.PNG
Figure 11. Installation of Prism library

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.

AppClass.PNG
Figure 12. App class setup

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.

xaml
Figure 13. App.xaml

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.

Conclusion

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:

https://github.com/PrismLibrary/Prism/releases/tag/7.0.0-Form

And if you like, you can support Prism development by becoming a Patron by visiting this link:

https://www.patreon.com/prismlibrary/memberships

Once again a big thanks to Bryan Lagunas, Dan Seigel and all Prism contributors for a job well done.

Happy Coding 🙂

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 )

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