MvvmCross Xamarin.Forms模态对话

时间:2017-07-24 00:43:07

标签: ios xamarin.ios xamarin.forms modal-dialog mvvmcross

我只是想用MvvmCross制作我的第一个应用程序,但我已经遇到了我的应用程序的问题,并且无法在网上找到任何内容。我想在支持的设备(例如iPad)上打开特定页面作为模式对话框。对于我的非MvvmCross应用程序,我使用Rg.Plugin.Popup执行此操作,但这次我不希望这样。我找到了一些关于如何用native和MvvmCross完成的解决方案,但没有关于如何将Xamarin.Forms与MvvmCross结合使用。我是否需要实现自己的演示者并过滤我的页面类型(因为此页面应始终在受支持的设备上模态化)?如果我这样做,有什么例子吗?

2 个答案:

答案 0 :(得分:2)

使用MvvmCross 5.2.0,以下内容适用于我(至少在iOS上;尚未在Android上测试过):

var bundle = new MvxBundle(new Dictionary<string, string>{ { MvxFormsPagePresenter.ModalPresentationParameter, "true" } });
await _navService.Navigate<MyViewModel>(bundle);

这使用了股票标准MvxFormsPagePresenter,因此您无需创建自己的演示者。

答案 1 :(得分:0)

我在这里使用MVVMCross 7.1.2,仅在Android上进行了测试。

在您的Views代码中,实施带有类似代码的IMvxOverridePresentationAttribute

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;
}

您现在所要做的就是,导航到视图模型时只需传递一个键值对作为这样的参数

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

就是这样...很容易!