issues in navigation bar back button and android back button code

sabeen_newbie 26 Reputation points
2021-02-02T10:25:35.167+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,353 questions
0 comments No comments
{count} vote

Accepted answer
  1. JarvanZhang 23,961 Reputation points
    2021-02-02T12:47:16.427+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.