I have a problem with xamarin.ios and MvvmCross, I need to display an MvxViewController and it comes in two ways depending on who calls it, I get it with:
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
};
}
}
}
If I do ViewModel.anything, to get a parameter to define the type of presentation, the ViewModel, is null and I can't access. I have not even opened it, since the type of presentation for this view is not defined.
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();
}
}
This is a restriction in MvvmCross, because the ViewModel is not loaded before the View is. This is also described in the documentation: 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With