我有一个导航服务,该服务接收一个viewModel,使用相应的视图创建一个页面,并将该页面推入导航堆栈。在此方法的最后,将调用viewModel的“ InitializeAsync(...)”方法。
protected virtual async Task InternalNavigateToAsync(Type viewModelType, object parameter)
{
var page = CreatePage(viewModelType, parameter);
Debug.WriteLine("\n\n\n\n\n" + page.ToString() + "\n\n\n\n\n");
if (page is MainView)
{
CurrentApplication.MainPage = page;
}
else if (CurrentApplication.MainPage is MainView)
{
var mainPage = CurrentApplication.MainPage as MainView;
if (mainPage.Detail is ScoreboardNavigationPage navigationPage)
{
var currentPage = navigationPage.CurrentPage;
if (currentPage.GetType() != page.GetType())
{
await navigationPage.PushAsync(page);
}
}
else
{
navigationPage = new ScoreboardNavigationPage(page);
mainPage.Detail = navigationPage;
}
mainPage.IsPresented = false;
}
else
{
var navigationPage = CurrentApplication.MainPage as ScoreboardNavigationPage;
if (navigationPage != null)
{
await navigationPage.PushAsync(page);
}
else
{
CurrentApplication.MainPage = new ScoreboardNavigationPage(page);
}
}
await (page.BindingContext as ViewModelBase).InitializeAsync(parameter);
}
首次使用MainViewModel调用此方法时,将执行Debug.Writeline(...)
并且一切正常。然后,触发MainViewModel的InitializeAsync(...)
方法,该方法通过HomeViewModel导航到HomeView:
public override async Task InitializeAsync(object data)
{
await Task.WhenAll
(
_menuViewModel.InitializeAsync(data),
_navigationService.NavigateToAsync<HomeViewModel>()
);
}
但是,导航到HomeView时,我的Debug.WriteLine(...)
永远不会触发!我尝试将断点放在Debug.WriteLine(...)
的上方和下方,但执行似乎在
var page = CreatePage(viewModelType, parameter);
这里是CreatePage(...)方法供参考:
protected Page CreatePage(Type viewModelType, object parameter)
{
Type pageType = GetPageTypeForViewModel(viewModelType);
if (pageType == null)
{
throw new Exception($"Mapping type for {viewModelType} is not a page");
}
Page page = Activator.CreateInstance(pageType) as Page;
return page;
}