我想使用选项卡式导航和MvvmCross制作一个Xamarin原生应用 . 我正在使用这个项目作为参考:

https://github.com/MvvmCross/MvvmCross/tree/master/TestProjects/Playground/Playground.iOS

我让项目工作并成功地将标签添加到我的应用程序,但是,我不明白它的工作原理 .

在应用程序启动时,我加载 TabsRootViewModel ,如下所示:

public class TabsRootViewModel : MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;

    public TabsRootViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService ?? throw new ArgumentNullException(nameof(navigationService));
        ShowInitialViewModelsCommand = new MvxAsyncCommand(ShowInitialViewModels);
    }

    // Init and Start are important parts of MvvmCross' CIRS ViewModel lifecycle
    // Learn how to use Init and Start at https://github.com/MvvmCross/MvvmCross/wiki/view-model-lifecycle
    public void Init()
    {
    }

    public override void Start()
    {
    }

    public IMvxAsyncCommand ShowInitialViewModelsCommand { get; private set; }

    private async Task ShowInitialViewModels()
    {
        var tasks = new List<Task>();
        tasks.Add(_navigationService.Navigate<Tab1ViewModel>());
        tasks.Add(_navigationService.Navigate<Tab2ViewModel>());
        tasks.Add(_navigationService.Navigate<Tab3ViewModel>());
        await Task.WhenAll(tasks);
    }
}

从我的iOS项目我有一个 TabsRootView ,看起来像:

public partial class TabsRootView : MvxTabBarViewController<TabsRootViewModel>
{
    private bool _isPresentedFirstTime = true;

    public TabsRootView() : base()
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        // Perform any additional setup after loading the view, typically from a nib.
    }

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        if (ViewModel != null && _isPresentedFirstTime)
        {
            _isPresentedFirstTime = false;
            ViewModel.ShowInitialViewModelsCommand.ExecuteAsync(null);
        }
    }
}

然后每个标签看起来像:

[MvxTabPresentation(TabName = "Tab1")]
public partial class Tab1View : MvxViewController
{
    public HistoryView() : base("Tab1View", null)
    {
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();

        // Release any cached data, images, etc that aren't in use.
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        // Perform any additional setup after loading the view, typically from a nib.
    }
}

所以, ViewDidLoad 调用 ViewModel 上的方法 ShowInitialViewModelsCommand ,这会以某种方式创建标签,正如我所料,它都能正常工作 .

我只是不明白 TabsRootViewModel 中的方法 ShowInitialViewModels 成功添加标签的方式或原因 . 我可以't really find any documentation out there that is up to date it seems. Can anybody explain how this is working? And if this is the correct way to accomplish what I'米试着吗?