![]() ![]() Once we get a hold of an implementation of that interface, we can do stuff like PushAsync, PopAync, PushModalAsync, … Every Page in MAUI has a Navigation property which is of type INavigation. How can we implement the navigation itself? Well, navigation in MAUI is done through the INavigation interface. Injecting such a NavigationService into our ViewModels is the easy part, thanks to MAUI’s baked-in DI container. I would like a NavigationService to be injected in my ViewModels, so that I can perform a navigation from a command that was triggered when the user tapped a button, for example. It should expose methods that can be called in order to navigate from one page to another, and passing parameters from one ViewModel to another. So how could we do some simple navigation? NavigationServiceĪ NavigationService should be responsible for…. How cool is this? Because of the built-in DI container in MAUI, we can make this all work without the need of any third party library! Public MainPageViewModel(IDataService dataService) ![]() The following code demonstrates how you can add a dependency of an IDataService interface to the MainPageViewModel, and how to register a concrete implementation of said interface. If you wouldn’t have registered a particular type which is a dependency of another type, a System.InvalidOperationException will be thrown, as you might expect. If said ViewModel would have a dependency (like a Service or Repository) on itself, which needs to be injected through the constructor, the DI container will (try to) resolve all of them. This means that when an instance of App is resolved, MainPage needs to be resolved and all of its dependencies, like in this example MainPageViewModel. public MainPage(MainPageViewModel viewModel) The only thing we have to do, is adding a constructor parameter to our MainPage, and make sure we register such a type ( MainPageViewModel for example) in our DI container. We can use this mechanism as well to inject a ViewModel into our View. So, to have DI working, we don’t need to do anything special because we already get this out of the box. If we now add a parameter of type MainPage to the constructor of the App class, the DI container will inject an instance when an the Builder resolves an instance of the App class: public App(MainPage mainPage) ![]() If we want, for example, to add our MainPage class to the DI container, we can do this as follows: public static class MauiProgramįonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular") Just like with ASP.NET Core applications, there’s now this ‘ Builder‘ that let’s you configure your application in a fluent style and which exposes a DI container through the Builder’s Services property. One of the awesome new things in MAUI is that we now have a DI container at our disposal out-of-the-box. Trigger events or call functions when navigating.Ability to pass parameters when navigating.Simple navigation from the ViewModels (only ‘linear’ navigation, forward, back, no tabs or drawer navigation).So the idea is to set up my own little ‘MVVM framework’ with the following features: Which, of course, is totally understandable: with all these preview releases rolling out, the maintainers of these projects have a ton of work to keep everything working, for which I have nothing but huge respect! The basic idea I’ve seen both frameworks doing great on earlier bits, but now on Preview 12 I’m not getting anything from them. ![]() I noticed that, at this moment of writing, both Prism and FreshMVVM aren’t quite there yet with MAUI. Both libraries are great to speed up your development process, as a lot of complex boilerplate code is already done for you! But -mostly for very small apps- I’ve previously also chose to not take a dependency on a third party library, and set up my app using my own little ‘framework’. In the past, I’ve been working with both Prism and FreshMVVM. I’ve done this multiple time in the past with Xamarin.Forms and I wanted to see how different this would be in MAUI. Last week I installed Preview 12 with the idea of creating a basic MVVM architecture for my future MAUI apps. The MAUI-team seems to be on a roll! We’ve seen multiple preview releases over the last months and with every release the product visibly improves! ![]()
0 Comments
Leave a Reply. |