James Lavery
James Lavery

Reputation: 949

MvvmCross initial IMvxViewModel ViewAppeared not being called when in an MvxTabbbedPage on Android

We have an MvxTabbedPage and child MvxContentPages in our Xamarin.Forms project.

On Android, I'm finding that the ViewAppeared override on my first child page is not being called the first time the MvxTabbedPage is being shown.

When switching tabs, it subsequently is being called correctly.

I'm initialising the PageModels in the ViewAppearing for the MvxTabbedPage's PageModel as below:

public override async void ViewAppearing()
{
    await ShowInitialViewModels();
    base.ViewAppearing();
}

private bool viewModelsInitialised = false;
private async Task ShowInitialViewModels()
{
    if (!viewModelsInitialised)
    {
        await _BusyManager.SetBusy();
        var tasks = new List<Task>();
        tasks.Add(_MvxNavigationService.Navigate<HomePageModel>());
        tasks.Add(_MvxNavigationService.Navigate<MyBenefitsPageModel>());
        tasks.Add(_MvxNavigationService.Navigate<ClaimsPageModel>());
        tasks.Add(_MvxNavigationService.Navigate<ContactUsPageModel>());
        tasks.Add(_MvxNavigationService.Navigate<SettingsPageModel>());
        await Task.WhenAll(tasks);
        viewModelsInitialised = true;
        await _BusyManager.SetUnBusy();
    }
}

Have others seen this behaviour, and/or should I be doing something differently?

Upvotes: 1

Views: 816

Answers (2)

FabriBertani
FabriBertani

Reputation: 1636

Check the Playground project of mvvmcross. You should manage your tabs initializations separately in a viewmodel and the XF view code behind.

public class YourTabsViewModel : MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;

    public YourTabsViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;

        ShowInitialViewModelsCommand = new MvxAsyncCommand(ShowInitialViewModels);
    }

    public IMvxAsyncCommand ShowInitialViewModelsCommand { get; private set; }

    private async Task ShowInitialViewModels()
    {
        var tasks = new List<Task>
        {
            tasks.Add(_navigationService.Navigate<HomePageModel>();
            tasks.Add(_navigationService.Navigate<MyBenefitsPageModel>());
            tasks.Add(_navigationService.Navigate<ClaimsPageModel>());
            tasks.Add(_navigationService.Navigate<ContactUsPageModel>());
            tasks.Add(_navigationService.Navigate<SettingsPageModel>());
        }

        await Task.WhenAll(tasks);
    }
}

And then on the code behind of your XF view

[MvxTabbedPagePresentation(TabbedPosition.Root, NoHistory = true)]
public partial class YourTabsPage : MvxTabbedPage<YourTabsViewModel>
{
    public YourTabsPage()
    {
        InitializeComponent();
    }

    private bool _firstTime = true;

    protected override void OnAppearing()
    {
        base.OnAppearing();

        if (_firstTime)
        {
            ViewModel.ShowInitialViewModelsCommand.ExecuteAsync(null);

            _firstTime = false;
        }
    }
}

Upvotes: 0

James Lavery
James Lavery

Reputation: 949

Looks like it's this Forms bug:

https://github.com/xamarin/Xamarin.Forms/issues/3855

which is referenced by this MvvmCross issue

https://github.com/MvvmCross/MvvmCross/issues/2823

(thanks to Pedro for pointing me in this direction on Slack:)

Upvotes: 2

Related Questions