MvxViewController.PresentationAttribute 在加载 ViewModel 之前调用

MvxViewController.PresentationAttribute called before ViewModel is loaded

我在使用 xamarin.ios 和 MvvmCross 时遇到问题,我需要显示一个 MvxViewController,它有两种方式,具体取决于调用它的人,我通过以下方式获得它:

自定义视图控制器:

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,我无法访问。我什至没有打开它,因为未定义此视图的表示类型。

自定义视图模型:

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

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

To override a presentation attribute at runtime you can implement the IMvxOverridePresentationAttribute in your view controller and determine the presentation attribute in the PresentationAttribute method like this:

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

If you return null from the PresentationAttribute the iOS View Presenter will fallback to the attribute used to decorate the view controller. If the view controller is not decorated with a presentation attribute it will use the default presentation attribute (a animated child presentation).

Note: Be aware that your ViewModel will be null during PresentationAttribute, so the logic you can perform there is limited here. Reason to this limitation is MvvmCross Presenters are stateless, you can’t connect an already instantiated ViewModel with a new View.