在加载ViewModel之前调用的MvxViewController.PresentationAttribute

时间:2017-08-21 09:28:35

标签: xamarin xamarin.ios mvvmcross

我遇到了xamarin.ios和MvvmCross的问题,我需要显示一个MvxViewController,它有两种取决于谁调用它,我得到它:

CustomViewController:

public partial class CustomViewController : MvxViewController<CustomViewModel>, IMvxOverridePresentationAttribute
{

    public CustomViewController() : base("CustomViewController", null)
    {

    }

    public MvxBasePresentationAttribute PresentationAttribute()
    {

        if (ViewModel.KindNavigation) //Here's the issue
        {
            return new MvxSidebarPresentationAttribute(MvxPanelEnum.Center, MvxPanelHintType.ResetRoot, true, MvxSplitViewBehaviour.Detail);
        }
        else
        {
            return new MvxModalPresentationAttribute
            {
                ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen,
                ModalTransitionStyle = UIModalTransitionStyle.CoverVertical
            };
        }
    }
}

如果我执行ViewModel.anything,要获取用于定义表示类型的参数,ViewModel为null,我无法访问。我甚至没有打开它,因为没有定义此视图的演示类型。

CustomViewModel:

public class CustomViewModel : MvxViewModel<string>, IDisposable
{
    private readonly IMvxNavigationService _navigationService;

    public CustomViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;
    }

    private bool _KindNavigation;
    public bool KindNavigation
    {
        get => _KindNavigation;
        set => SetProperty(ref _KindNavigation, value);
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public override Task Initialize(string parameter)
    {
        KindNavigation = Convert.ToBoolean(parameter);
        System.Diagnostics.Debug.WriteLine("parameter: " + parameter);

        return base.Initialize();
    }
}

1 个答案:

答案 0 :(得分:1)

这是MvvmCross中的限制,因为在View之前未加载ViewModel。文档中也对此进行了描述:https://www.mvvmcross.com/documentation/presenters/ios-view-presenter?scroll=446#override-a-presentation-attribute-at-runtime

  

要在运行时覆盖表示属性,可以在视图控制器中实现IMvxOverridePresentationAttribute,并在PresentationAttribute方法中确定表示属性,如下所示:

public MvxBasePresentationAttribute PresentationAttribute()
{
    return new MvxModalPresentationAttribute
    {
        ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen,
        ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve
    };
}
  

如果从PresentationAttribute返回null,iOS View Presenter将回退到用于装饰视图控制器的属性。如果视图控制器未使用表示属性进行修饰,则它将使用默认的表示属性(动画子表示法)。

     

注意:请注意,在PresentationAttribute期间,您的ViewModel将为null,因此您可以在此处执行的逻辑有限。此限制的原因是MvvmCross Presenters是无状态的,您无法使用新视图连接已经实例化的ViewModel。