工具栏后退在 Xamarin android 中不起作用 [英] Toolbar back click is not working in Xamarin android

查看:55
本文介绍了工具栏后退在 Xamarin android 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了带有后退箭头的工具栏,单击不起作用

I have Created Toolbar with back arrow, click is not working

 toolbar = FindViewById<Toolbar>(Resource.Id.toolbar2);
 toolbar.NavigationClick += Back;
 private void Back(object sender, Toolbar.NavigationClickEventArgs e)
 {
    Finish();
 }

推荐答案

在你的 OnCreate 方法中这样做:

In your OnCreate method do this :

ActionBar.SetHomeButtonEnabled(true);
ActionBar.SetDisplayHomeAsUpEnabled(true);

然后像这样覆盖 OnOptionsItemSelected 方法.

Then override the OnOptionsItemSelected method like this.

public override bool OnOptionsItemSelected(IMenuItem item)
 {
   switch (item.ItemId)
    {
         case Android.Resource.Id.Home:
         Finish();
         return true;

       default:
       return base.OnOptionsItemSelected(item);
    }
  }

如果您使用的是 Xamarin.Android.Support.v7Android.Resource.Id.Home 应该是 Resource.Id.Home.

If you are using Xamarin.Android.Support.v7 the Android.Resource.Id.Home should be Resource.Id.Home.

此外,您只需要 ActionBar.SetDisplayHomeAsUpEnabled(true); 来显示主页按钮,不需要 ButtonEnabled 调用.

Also, you only need ActionBar.SetDisplayHomeAsUpEnabled(true); to show the Home button, no need for the ButtonEnabled call.

在 OnCreate 中是这样的:

Something like this in OnCreate :

Toolbar toolbar = FindViewById<Toolbar>(Resource.Id.toolbarID);
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);

OnOptionsItemSelected 中的类似内容:

And something like this in OnOptionsItemSelected:

public override bool OnOptionsItemSelected(IMenuItem item)
  {
switch (item.ItemId)
  {
  case Android.Resource.Id.Home:
  Finish();
  return true;

      default:
  return base.OnOptionsItemSelected(item);
  }
  }

这篇关于工具栏后退在 Xamarin android 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆