Toolbar back click is not working in Xamarin android

我的梦境 提交于 2019-12-12 18:26:14

问题


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();
 }

回答1:


In your OnCreate method do this :

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

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);
    }
  }

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

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

Something like this in OnCreate :

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

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);
  }
  }


来源:https://stackoverflow.com/questions/50620799/toolbar-back-click-is-not-working-in-xamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!