【发布时间】:2021-12-03 18:16:04
【问题描述】:
我正在尝试覆盖工具栏中的软件后退按钮,但显然没有简单的方法。目前,我正在尝试自己做一些事情......不知何故......
看起来我需要 OnOptionsItemSelected 方法,但由于某种原因它没有被调用。
这是我的 OnOptionsItemSelected 覆盖,忽略里面的东西..
public override bool OnOptionsItemSelected(IMenuItem item) {
// check if the current item id is equals to the back button id
if (item.ItemId == 16908332)
{
// retrieve the current xamarin forms page instance
var selected = item;
// Some random stuff will come here.
return base.OnOptionsItemSelected(item);
}
else
{
// since its not the back button click, pass the event to the base
return base.OnOptionsItemSelected(item);
}
我看到你需要在 MainActivity 的 OnCreate 方法中做一些“初始化”才能让它运行:
if (FindViewById(Resource.Id.toolbar) is AndroidX.AppCompat.Widget.Toolbar toolbar)
{
SetSupportActionBar(toolbar);
}
但由于某种原因,我无法“找到”这个 AndroidX.AppCompat.Widget.Toolbar。由于某种原因,AppCompat 不存在。我尝试安装 Xamarin.AndroidX.AppCompat,但仍然找不到。有什么想法吗?
【问题讨论】:
-
顶部的后退按钮需要与Android原生的后退按钮有不同的行为吗?
-
我想实现“未保存更改,您确定要返回吗?”东西,你
-
是的,但是,如果用户点击Android的BackButton,本机(下图)会做同样的事情还是你想一个人做某事,另一个人做另一件事?
-
哦,你是说设备的“硬件”按钮?我通过重写 OnBackButtonPressed() 方法来处理它。 (他们应该做同样的事情,是的)
-
覆盖类中的 OnBackButtonPressed() 或 mainActicity 中的 OnBackPressed() 仅适用于硬件按钮(底部的 android 按钮),但对工具栏中的“软件”按钮无效...知道如何在 mainActicity 中“调用”OnOptionsItemSelected()?
标签: c# android .net xamarin xamarin.forms