question

sabeennewbie-6230 avatar image
1 Vote"
sabeennewbie-6230 asked 22601544 commented

issues in navigation bar back button and android back button code

I am trying to override navigation bar back button and back button on the andriod application. i used a document and the code provided with it in the github and i succeeded partially.

problem number 1. i needed to make a custom class inherited from content page and derive all my view classes from my custom class. for that i made some changes in their xaml file. there are no errors and codes runs but no views are displayed on the page. in the xaml file i can see a warning but i dont know how to cater it. here is my code

Custom.cs

namespace MyProjects
{
public class CustomPage : ContentPage
{
/// <summary>
/// Gets or Sets the Back button click overriden custom action
/// </summary>
........

MainPage.xaml.cs

namespace MyProjects.Views
{
public partial class MainPage : CustomPage
{

     public MainPage()
     {
         InitializeComponent();
         BindingContext = new MainPageViewModel();
            
     }

MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<d:CustomPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="clr-namespace:MyProjects;assembly=MyProjects"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:MyProjects.Views" xmlns:d1="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewmodels="clr-namespace:MyProjects.ViewModels"
xmlns:locals="clr-namespace:MyProjects"
mc:Ignorable="d"
x:Class="MyProjects.Views.MainPage"
Title="My Project -- Project Page"
IconImageSource="check2x.png" >

![62842-datanotsetxaml.png][1]

i have attached the screen shot of the warning that i am getting in my xaml file. i think removing this warning will solve the issue. please help me, thx.

problem number 2:
in order to override Navigation bar back button, i have written code in method OnOptionsItemSelected in the MainActivity.cs file. this method is never called whenever back button event is occurred. i am trying to call SetSupportActionBar() but it is giving error. please let me know why cant i call this method for the toolbar view. i am also downloaded certain nuget packages but nothing is working. here is my code.

MainActivity.cs

 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
 {
     protected override void OnCreate(Bundle savedInstanceState)
     {
         TabLayoutResource = Resource.Layout.Tabbar;
         ToolbarResource = Resource.Layout.Toolbar;

       .........
       ..........
     Android.Support.V7.Widget.Toolbar toolbar
             = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
             SetSupportActionBar(toolbar);
     }

     public override bool OnOptionsItemSelected(IMenuItem item)
     {
         // check if the current item id 
         // is equals to the back button id
         if (item.ItemId == 16908332)
         {
                            //my code
                      }
              }
         }

the solution is made in VS2019, project type is xamarin.form
thank u v much.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered 22601544 commented

Hello,​

Welcome to our Microsoft Q&A platform!

this method is never called whenever back button event is occurred. i am trying to call SetSupportActionBar() but it is giving error

What's the version of the Xamarin.Forms in your project? Xamarin.Forms 5.0 uses the AndroidX library by default. If the Target Framework of the Android platform project is set to Android 10, please use the AndroidX api instead. And place the SetSupportActionBar code after the LoadApplication(new App()) line.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        base.OnCreate(savedInstanceState);
        ...
        LoadApplication(new App());
        AndroidX.AppCompat.Widget.Toolbar toolbar = this.FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);
    }
    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        return base.OnOptionsItemSelected(item);
    }
    ...
}


For the first problem, I don't see the screenshot. Please re-post the file and share more details about the error.

Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,

I have same problem. If I call SetSupportActionBar after LoadApplication, OnOptionsItemSelected works but my ToolBarItems stop been displayd. If navigate to some page and navigate back, ToolBarItems appear

using Xamarin 5.0.0.2012, platform project is set to Android 11

Good luck

0 Votes 0 ·