我的MasterPage当前使用事件驱动方法,如下所示:
MasterPage.xaml.cs
private void TapHome_Tapped(object sender, EventArgs e)
{
Detail = new NavigationPage(new HomePage());
IsPresented = false;
}
App.xaml.cs:
public App()
{
InitializeComponent();
if (!string.IsNullOrEmpty(Preferences.Get(Constant.Setting_AccessToken, "")))
{
MainPage = new MasterPage();
}
else if (string.IsNullOrEmpty(Preferences.Get(Constant.Setting_UserEmail, "")) &&
string.IsNullOrEmpty(Preferences.Get(Constant.Setting_Password, "")))
{
MainPage = new NavigationPage(new LoginPage());
}
}
由于我们尝试迁移到MVVM方法。我们试图更改代码:
MasterPage.xaml.cs
public MasterPage()
{
InitializeComponent();
BindingContext = _viewModel = new MasterViewModel()
{
Navigation = Navigation
};
//IsPresented = false;
}
MasterViewModel.cs:
public ICommand HomeCommand { get; private set; }
public ICommand ActivitiesCommand { get; private set; }
public ICommand ChangePasswordCommand { get; private set; }
public ICommand LogoutCommand { get; private set; }
public MasterViewModel()
{
HomeCommand = new Command(async () => await GoToHome());
ActivitiesCommand = new Command(async () => await GoToActivities());
ChangePasswordCommand = new Command(async () => await GoToChangePassword());
LogoutCommand = new Command(async () => await Logout());
}
public async Task GoToHome()
{
// Attempt 1:
// Navigation.PushAsync(new NavigationPage(new HomePage()));
// Attempt 2:
// Application.Current.MainPage = new NavigationPage(new HomePage());
}
尝试了第一次尝试,我们在Android上出错- System.InvalidOperationException:Android上不全局支持PushAsync,请使用NavigationPage。
尝试了第二次尝试,并且工作正常,但单击后在主页或其他页面上没有导航。
有什么想法吗?
答案 0 :(得分:0)
没关系。我们解决了这个问题:
public async Task GoToHome()
{
((MasterDetailPage)Application.Current.MainPage).Detail = new NavigationPage(new HomePage());
((MasterDetailPage)Application.Current.MainPage).IsPresented = false;
}