Agat
Agat

Reputation: 4779

What's the way to achieve conditional presentation for view model with MvvmCross?

I have a login viewmodel (with corresponding page). In this particular situation I am using Xamarin.Forms.

What I need is the login view to be presented as common view of Navigation Stack as the view which could be annotated with [MvxModalPresentationAttribute].

I show this view in two cases:

I guess, Custom Presenter is the way to achieve that, like this (for iOS, as example):

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

        base.Show(request);
    }
}

However, I am not quite following what should be next steps. (Especially, if there is anything specific in regards to Xamarin.Forms should be also done).

Any hints?

Upvotes: 2

Views: 1004

Answers (1)

Ax1le
Ax1le

Reputation: 6643

on Mvvmcross.core 5.7.0, If you want to present a view with modal style on iOS, you can add a MvxModalPresentation attribute to the view:

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

Then the way to present this view is the same as push:

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

At last dismiss this view should be like:

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

Update:

After updating the Mvvmcross to 6.0.1.0, we can use the IMvxOverridePresentationAttribute interface to define a view's presentation style. Make the view implement the interface:

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 is defined in my ViewModel. when we want to present a view, use this to modify the style:

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

Upvotes: 5

Related Questions