首页 > 解决方案 > 用 MvvmCross 实现视图模型的条件表示的方法是什么?

问题描述

我有一个登录视图模型(带有相应的页面)。在这种特殊情况下,我使用的是 Xamarin.Forms。

我需要的是作为导航堆栈的通用视图呈现的登录视图,作为可以用 [MvxModalPresentationAttribute] 注释的视图。

我在两种情况下展示了这种观点:

我想,Custom Presenter 是实现这一目标的方法,就像这样(对于 iOS,例如):

public class GeneralPresenter : MvxIosViewPresenter
{
    public override void Show(MvxViewModelRequest request)
    {
        // ...

        base.Show(request);
    }
}

但是,我并没有完全遵循下一步应该采取的措施。(特别是,如果有任何关于 Xamarin.Forms 的特定内容,也应该完成)。

有什么提示吗?

标签: xamarin.formsxamarin.iosxamarin.androidmvvmcross

解决方案


在 Mvvmcross.core 5.7.0 上,如果你想在 iOS 上呈现一个模态样式的视图,你可以MvxModalPresentation给视图添加一个属性:

[MvxModalPresentation(
        // Add this to modify the present view's style
        //ModalPresentationStyle = UIModalPresentationStyle.PageSheet,
        //ModalTransitionStyle = UIModalTransitionStyle.CoverVertical
    )]
public class SecondView : MvxViewController
{
    ...
}

那么这个视图的呈现方式和push一样:

private readonly Lazy<IMvxNavigationService> _navigationService = new Lazy<IMvxNavigationService>(Mvx.Resolve<IMvxNavigationService>);
async private void ExecuteCommand()
{
    await _navigationService.Value.Navigate<SecondViewModel>();
}

最后驳回这个观点应该是这样的:

async private void ExecuteCommand()
{
    await _navigationService.Value.Close(this);
}

更新:

将 Mvvmcross 更新到 6.0.1.0 后,我们可以使用该IMvxOverridePresentationAttribute接口来定义视图的呈现样式。使视图实现接口:

public class SecondView : MvxViewController<SecondViewModel>, IMvxOverridePresentationAttribute
{
    ...
    public MvxBasePresentationAttribute PresentationAttribute(MvxViewModelRequest request)
    {
        var instanceRequest = request as MvxViewModelInstanceRequest;
        SecondViewModel viewModel = instanceRequest?.ViewModelInstance as SecondViewModel;

        if (viewModel.IsModalView)
        {
            return new MvxModalPresentationAttribute();
        }
        return new MvxChildPresentationAttribute();
    }
    ...
}

IsModalView在我的 ViewModel 中定义。当我们想要呈现一个视图时,使用它来修改样式:

public class SecondViewModel : MvxViewModel<bool>
{
    ...
    public override void Prepare(bool parameter)
    {
        IsModalView = parameter;
    }
    public bool IsModalView { set; get; }
    ...
}
// The navigate method
await _navigationService.Value.Navigate<SecondViewModel, bool>(false);

推荐阅读