我想使用标签式导航和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
调用ShowInitialViewModelsCommand
上的方法ViewModel
,这会以某种方式创建我期望的标签,并且一切正常。
我只是不明白ShowInitialViewModels
中的方法TabsRootViewModel
成功添加标签的方式或原因。我真的找不到任何最新的文档。任何人都能解释一下这是如何工作的吗?如果这是完成我想要做的事情的正确方法吗?