[英]How to achieve Android standard navigation with Xamarin 4 Shell Navigation?
在 Android 应用程序中使用导航抽屉(即汉堡菜单)时,对于大多数用户来说,Xamarin Shell 导航具有硬件后退按钮的令人困惑的默认行为。
每当我从菜单导航到一个页面 (FlyoutItem) 然后点击硬件后退按钮时,应用程序就会关闭,手机又回到启动器屏幕。 但我认为后退按钮应该转到最后显示的屏幕或开始屏幕。
来自 Google 的应用程序,如 Play 商店或 Gmail 或索尼的相册应用程序,在点击硬件后退按钮时将导航回应用程序的开始屏幕。 然后点击开始屏幕上的后退按钮将关闭应用程序。 这是我对 Shell 导航的预期行为。 Gmail 和相册总是显示汉堡菜单图标,其他人用后退图标/箭头替换汉堡。
使用硬件后退按钮时,进入起始页的首选方式是什么?
到目前为止我认为的一个选项是覆盖OnBackButtonPressed
中的xaml.cs
。 可以使用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 类中覆盖 OnBackPressed。 在该方法中,我使用消息中心发送“BackButtonPressed”消息。 在我的 shell 视图模型中,我订阅了这条消息,并在收到后导航到我的主页。
在主活动中:
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");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.