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.