MvvmCross Xamarin.Forms Modal Dialogs

Viewed 1960

I'm just trying to make my first app with MvvmCross but I already encounter a problem for my app and can't find anything on the web for that. I want to open a specific page as a modal dialog on supported devices (eg. iPads). For my non MvvmCross applications I do this with Rg.Plugin.Popup but I do not want it this way this time. I found a few solutions on how it can be done with native and MvvmCross but nothing about how it is done with Xamarin.Forms in combination to MvvmCross. Did I need to implement a own presenter and filter on the type of my page (because this page should always be modal on supported devices)? And if I do so, is there any example for this?

2 Answers

I'm using MVVMCross 7.1.2 here, this was tested on Android only.

In your Views code behind implement IMvxOverridePresentationAttribute with code similar to this.

public MvxBasePresentationAttribute PresentationAttribute(MvxViewModelRequest request)
{
    if (request.PresentationValues == null) return null;

    if (request.PresentationValues.ContainsKey("NavigationMode") &&
        request.PresentationValues["NavigationMode"] == "Modal")
    {
        return new MvxModalPresentationAttribute
        {
            WrapInNavigationPage = true,
            Animated = false,
            NoHistory = true
        };
    }

    return null;
}

All you need to do now, is when Navigating to the view model is just pass a key value pair as the parameter like this

await NavigationService.Navigate<MainViewModel>(new MvxBundle(new Dictionary<string, string> { { "NavigationMode", "Modal" } }));

That's it... easy as that !

Related