TinyMvvm 2.1.1 – Introducing ViewModel navigation

Today I released TinyMvvm 2.1.1. The release contains the following:

  • Updated Xamarin.Forms dependency so it has dependency on the lastest stable version of Xamarin.Forms.
  • ViewModel navigation

ViewModel navigation

The idea about ViewModel navigation is from Shane Neuville from the Xamarin.Forms team, he reviewed TInyMvvm in one of his live streams on Twitch. During the stream he walktrough the code and he also wrote code for ViewModel Navigation. After the stream he sent me the code. For the 2.1.1 release I have added his code to the repopsitory. Thank you Shane for contributing to TinyMvvm!

To use ViewModel Navigation you need to register view by running the RegisterViewInAssembly method and pass the Assembly where your views are located. ViewModel navigation can only be used with the ShellNavigationHelper and for Views that has ViewBase<TViewModel> as its base class.

var navigationHelper = new ShellNavigationHelper();
navigationHelper.RegisterViewsInAssembly(Assembly.GetExecutingAssembly());

Then you can navigate like this in your ViewModel to navigate to for example the AboutView with that uses AboutViewModel:

await Navigation.NavigateToAsync(nameof(AboutViewModel));
//or with query parameter,
await Navigation.NavigateToAsync($"{nameof(AboutViewModel)}?id=1");

The pros by using this is that you don't need to handle keys in your ViewModel and you don't either need to reference the views to use nameof(AboutView) as the key.

Read more

If you don't have used TinyMvvm before I recomend you to read this blog post.

  5/28/2020 - 9:49 PM