The OpenNETCF IoC library has been around for a long time. I needed a DI container that was simple and fast that provided some cool features like event aggregation and module plugins. I still use it heavily in every project I work on.
When I started working with Xamarin Forms, I still needed a DI container. Now I know that Xamarin provides one, but I already know how to use OpenNETCF’s IoC and the syntax for it is pretty much muscle memory for me at this point. I didn’t want to learn a new paradigm when the one I have solves the problem just fine and it will let me continue to use the same syntax on Windows desktop, Web APIs, Xamarin and still the occasional Windows CE project.
The lack of Reflection support in a PCL assembly forced me to rework things a bit, so I lost event aggregation and plug-ins, but it’s still a really simple and fast DI container that supports both singletons and named instances.
What was missing, however, was a navigation structure. So I created one and added it to the Xamarin build of IoC. Here’s a very, very quick overview of how you can use it.
The NavigationService class is the root of what you’ll you and it’s a static to make it easier to use. I don’t want or need to create or find an instance of it every time I want to use it.
The first step is to create a View/ViewModel pair. The View must derive from Page. The ViewModel must implement the provided IViewModel interface (which simply implements INotifyPropertyChanged and nothing else at this point). Basically it’s a near drop-in for any Xamarin Forms project.
So let’s assume I have a simple app with two pages: Home and Detail.
I’d create the following classes: HomeView, HomeViewModel, DetailsView and DetailsViewModel. The Views are just the out-of-the-box Pages. The ViewModels would simply derive from IViewModel and add in the PropertyChanged event (which you probably have already).
Setting things up for using the Navigation service is straightforward. You simply register the View/ViewModel types. Note that you don’t actually create any instances of the Views or ViewModels:
This registration tells the NavigationService the relationship so that when you need to display a View, it can ensure that the ViewModel also exists and then it can inject it as the BindingSource for you.
Once we’ve registered our Views and ViewModels, we can start up the app like this:
Things to note here are that we don’t directly set the Application.MainView, we simply tell the navigation service the Type we want to use. We also pass in true here as a parameter to tell the service we want it to wrap the Page in a NavigationPage for us. If your MainView already derives from NavigationPage, you’d pass in false. Yes, this could probably be made automatic, but for now it’s not.
Now that you have you main page, you can navigate between pages forward and back. For example, to go from the MainView to the DetailsView, you’d use this (true telling the NavigationService to show animation for the navigation):
And if you’re on the DetailsView and you want to go back? That’s as simple as this: