Skip to content Skip to sidebar Skip to footer

Xamarinforms Appcompat Onoptionsitemselected

I have recently updated xamarin forms to 1.5.1-pre1 so that I can use the beautiful AppCompat themes. It works and looks very nice. I do have one problem, in my old FormsApplicatio

Solution 1:

You can add the following to your custom renderer. You can either user current activity plugin or cast your context to activity.

var toolbar =CrossCurrentActivity.Current?.Activity?.FindViewById<Toolbar>(Resource.Id.toolbar);

toolbar.NavigationClick+=ToolbarNavigationClick;

Solution 2:

Have a look at the last two lines of the OnCreate() After adding them OnOptionsItemSelected was called as expected).

https://raw.githubusercontent.com/UdaraAlwis/Xamarin-Playground/master/XFNavBarBackBtnClickOverride/XFNavBarBackBtnClickOverride/XFNavBarBackBtnClickOverride.Droid/MainActivity.cs

Toolbartoolbar=this.FindViewById<Toolbar>(Resource.Id.toolbar);

        SetSupportActionBar(toolbar);

Solution 3:

In Xamarin.Forms there's a better way to intercept the NavigationBar back button pressed and the hardware back button pressed, which is creating your own NavigationRenderer and overriding the method OnPopViewAsync:

[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationRenderer))]
namespaceYourApp.Droid
{
    publicclassCustomNavigationRenderer : NavigationPageRenderer
    {
        publicCustomNavigationRenderer(Context context) : base(context)
        {
        }

        protectedoverrideasync Task<bool> OnPopViewAsync(Page page, bool animated)
        {
            // Write your code here
        }
    }
}

Hope this helps

Post a Comment for "Xamarinforms Appcompat Onoptionsitemselected"