如何使用 Xamarin 4 Shell 导航实现 Android 标准导航?
How to achieve Android standard navigation with Xamarin 4 Shell Navigation?
在 Android 应用程序中使用导航抽屉(即汉堡包菜单)时,Xamarin Shell 导航对大多数用户来说硬件后退按钮的默认行为令人困惑。
每当我从菜单导航到页面 (FlyoutItem) 然后点击硬件后退按钮时,应用程序关闭并且 phone 返回启动器屏幕。但我认为后退按钮应该转到最后显示的屏幕或开始屏幕。
Google 中的应用程序,如 Play Store、Gmail 或 Sony 的相册应用程序,在按下硬件后退按钮时将导航回应用程序的开始屏幕。然后点击开始屏幕上的后退按钮将关闭该应用程序。这是我预期的 Shell 导航行为。 Gmail 和 Album 总是显示汉堡包菜单图标,其他的用背面代替汉堡包图标 icon/arrow。
使用硬件后退按钮时,进入起始页的首选方式是什么?
到目前为止我想到的一个选项是覆盖 xaml.cs
中的 OnBackButtonPressed
。 Shell
的BackButtonBehavior
可以用吗?
而不是 FlyoutItem
使用 MenuItem
例如:AppShell.xaml
<Shell....>
<MenuItem Text="bears"
Icon="bear.png"
Command="{Binding BearsPageCommand}" />
.
.
.
</Shell>
AppShell.xaml.cs
public ICommand BearsPageCommand => new Command(async () => await NavigateToBearsPageAsync());
async Task NavigateToBearsPageAsync()
{
ShellNavigationState state = Shell.Current.CurrentState;
await Shell.Current.Navigation.PushAsync(new BearsPage());
Shell.Current.FlyoutIsPresented = false;
}
您可以在 Android MainActivity class 中覆盖 OnBackPressed。在该方法中,我使用消息中心发送 'BackButtonPressed' 消息。然后在我的 shell 视图模型中订阅此消息,并在收到后导航到我的主页。
在 MainActivity 中:
public override void OnBackPressed()
{
//currently hitting back button terminates the whole app, not navigating back
//instead let's make it navigate back to the home page and not close the app!
//also serves as workaround for the Shell exception on back button pressed
var msg = new BackButtonPressedMessage();
MessagingCenter.Send(msg, "BackButtonPressedMessage");
}
然后在视图模型中:
void WireUpBackButon()
{
//listen for hardware back button presses from Android
MessagingCenter.Subscribe<BackButtonPressedMessage>(this, "BackButtonPressedMessage", async message =>
{
await Shell.Current.GoToAsync("//home");
});
}
在 Android 应用程序中使用导航抽屉(即汉堡包菜单)时,Xamarin Shell 导航对大多数用户来说硬件后退按钮的默认行为令人困惑。
每当我从菜单导航到页面 (FlyoutItem) 然后点击硬件后退按钮时,应用程序关闭并且 phone 返回启动器屏幕。但我认为后退按钮应该转到最后显示的屏幕或开始屏幕。
Google 中的应用程序,如 Play Store、Gmail 或 Sony 的相册应用程序,在按下硬件后退按钮时将导航回应用程序的开始屏幕。然后点击开始屏幕上的后退按钮将关闭该应用程序。这是我预期的 Shell 导航行为。 Gmail 和 Album 总是显示汉堡包菜单图标,其他的用背面代替汉堡包图标 icon/arrow。
使用硬件后退按钮时,进入起始页的首选方式是什么?
到目前为止我想到的一个选项是覆盖 xaml.cs
中的 OnBackButtonPressed
。 Shell
的BackButtonBehavior
可以用吗?
而不是 FlyoutItem
使用 MenuItem
例如:AppShell.xaml
<Shell....>
<MenuItem Text="bears"
Icon="bear.png"
Command="{Binding BearsPageCommand}" />
.
.
.
</Shell>
AppShell.xaml.cs
public ICommand BearsPageCommand => new Command(async () => await NavigateToBearsPageAsync());
async Task NavigateToBearsPageAsync()
{
ShellNavigationState state = Shell.Current.CurrentState;
await Shell.Current.Navigation.PushAsync(new BearsPage());
Shell.Current.FlyoutIsPresented = false;
}
您可以在 Android MainActivity class 中覆盖 OnBackPressed。在该方法中,我使用消息中心发送 'BackButtonPressed' 消息。然后在我的 shell 视图模型中订阅此消息,并在收到后导航到我的主页。
在 MainActivity 中:
public override void OnBackPressed()
{
//currently hitting back button terminates the whole app, not navigating back
//instead let's make it navigate back to the home page and not close the app!
//also serves as workaround for the Shell exception on back button pressed
var msg = new BackButtonPressedMessage();
MessagingCenter.Send(msg, "BackButtonPressedMessage");
}
然后在视图模型中:
void WireUpBackButon()
{
//listen for hardware back button presses from Android
MessagingCenter.Subscribe<BackButtonPressedMessage>(this, "BackButtonPressedMessage", async message =>
{
await Shell.Current.GoToAsync("//home");
});
}