Just over 2 months ago I released the first preview of the new Prism for Xamarin.Forms. Since then, I have been hard at work gathering community feedback, refactoring the code base, and implementing new features. Today I am happy to announce the next preview version of Prism for Xamarin.Forms 5.7.0 is now available on NuGet now.
Let’s take a quick look at the newest improvements and features added to Prism for Xamarin.Forms.
The first improvement I have made is related to how we obtain the correct INavigationService in our ViewModels for navigation. In 5.6.1, you had to have your ViewModel implement the INavigationServiceAware interface. This interface defined a single NavigationService property that would be set during the creation of the ViewModel.
Well, this worked and all, but really isn’t the ideal way to get the NavigationService. While using INavigationServiceAware enabled you to keep your ViewModels testable, it would hide the dependencies of the ViewModel. Which means, you had to have knowledge of the interface and the dependency on the NavigationSrvice property in order to properly test. This is not what we want.
Breaking Change: INavigationServiceAware has been removed.
Now, with Prism for Xamarin.Forms 5.7.0, you can now inject the INavigationService directly into the constructor of your ViewModels.
Important: In order for Prism to inject the proper INavigationService via the constructor, you must name the parameter “navigationService”.
Now, this is much better. We are now completely testable and the dependencies of our ViewModels are known based on the constructor signature.
The next improvement I made was to how you register your types for navigation as well as how you navigate to them. I have added an overload for IUnityContainer.RegisterTypeForNavigation which now accepts two types in the generic signature. The first argument is for the View Type, the second argument is of a Class Type, most likely a ViewModel.
There was also a corresponding change made to the INavigationService.Navigate method to accept a generic in place of a string to identify the View to navigate to. This will be useful for those devs that like to eliminate those nasty magic strings, and conceptually navigate to ViewModels instead.
If you have ever used the Xamarin.Forms DependencyService, you have probably beaten your head against the wall trying to test your ViewModels that make a call to it. What’s so bad about it? You have to make a static method call off of the DependencyService class. This is no good, especially for those of you who like to write testable ViewModels.
You might be asking yourself, “so how does Prism improve this experience?”. Well, I’m glad you asked, because I am about to blow your mind, as well as make your development and testing life much easier. You can now ask for your dependency as an argument in your ViewModel constructor. You heard me! Just ask for it, and Prism will give it to you. You don’t have to do anything special. It just works!
As long as you have done the necessary steps to register your platform specific services with Xamarin.Forms DependencyService, then it will just work with no extra code from you. Actually, it eliminates code you would have normally had to write. Check out this sample app that demonstrates how it works using the ITextToSpeech sample provided by Xamarin.
Well, that does it for this post. I hope you enjoy these new features and like the direction Prism for Xamarin.Forms is going. As always, feel free contact me on my blog, connect with me on Twitter (@brianlagunas), or leave a comment below for any questions or comments you may have.