Skip to content

Xamarin.Forms Navigation specials

Jan M edited this page Nov 20, 2020 · 2 revisions

Using Master-Detail Page

Using TabbedPage

Registering a navigation route for a specific platform

There are scenarios in which you have to register the same ViewModel with different views for different platforms, e.g. because the look should differ on Android and iOS to have a native feeling. CodeMonkeys Navigation service for Xamarin.Forms supports two different approaches for this.

You can either create a NavigationRegistration instance and set the Platform property:

var androidMainRoute = new NavigationRegistration<MainViewModel, MainViewAndroid>
{
    Platform = DevicePlatforms.Android
};

var iOSMainRoute = new NavigationRegistration<MainViewModel, MainViewiOS>
{
    Platform = DevicePlatforms.iOS
};

_navigationService.Register(androidMainRoute);
_navigationService.Register(iOSMainRoute);

or use the extensions methods in fluent style:

navigationService.Register<MainViewModel, MainViewAndroid>()
    .OnPlatform(DevicePlatforms.Android);

navigationService.Register<MainViewModel, MainViewAndroid>()
    .OnPlatform(DevicePlatforms.iOS);

Please note that the DevicePlatforms enum is tagged with the Flags attribute and it is possible to specify multiple values:

navigationService.Register<MainViewModel, MobileMainView>
    .OnPlatform(DevicePlatforms.Android | DevicePlatforms.iOS);

navigationService.Register<MainViewModel, MainViewWindows>
    .OnPlatform(DevicePlatforms.Windows);
Clone this wiki locally