In Page-A, I write a command to direct to Page-B using
App.Current.MainPage.Navigation.PushAsync(new B_Page())
The original behavior of the back button's action(on the left-top of the page) is to go back to the last page(Page-A). -->Action_A
And now, I want to rewrite the behavior to change page to a new page(called Page-C) by pressing the back button. -->Action_B
using Xamarin.Forms.Application.Current.MainPage = new NavigationPage(new C_Page());
But I want to keep both of these behaviors, because I will use each of them in different scenarios.
How to retain both behaviors and how to make me specify which action I want to use?
Let me use Action_A in scenario A and use Action_B in scenario B.
Thanks for the kind reply.
You can Manage Above Scenario with this Override Method
`
protected override bool OnBackButtonPressed()
{
//here you can manage your Navigation.
return base.OnBackButtonPressed();
}
`
We could re-write the Navigation View of the ContentPage.And re-write the back button as you want.
Create a BaseContentPage
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace xxx
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BaseContentPage: ContentPage
{
public event EventHandler BackButtonAction;
public BaseContentPage()
{
InitializeComponent();
NavigationPage.SetHasBackButton(this, false);
TitleLab.Text = Title;
}
private void Button_Clicked(object sender, EventArgs e)
{
this.BackButtonAction(sender,e);
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="xxx.BaseContentPage">
<NavigationPage.TitleView>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.2*" />
<ColumnDefinition Width="0.6*" />
<ColumnDefinition Width="0.2*" />
</Grid.ColumnDefinitions>
<!--you could set the icon or image here as back button-->
<Button BackgroundColor="Transparent" TextColor="White" Grid.Column="0" Text="Back" Clicked="Button_Clicked" />
<Label x:Name="TitleLab" Grid.Column="1" TextColor="White" FontSize="18" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
</Grid>
</NavigationPage.TitleView>
</ContentPage>
And you just need to create the subclass of the BaseContentPage
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace App11
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PageA : Page1
{
public PageA()
{
InitializeComponent();
this.BackButtonAction += PageA_BackButtonAction;
}
private void PageA_BackButtonAction(object sender, EventArgs e)
{
//do something you want
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<local:Page1 xmlns:local="clr-namespace:App11" xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App11.PageA">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</local:Page1>
You could handle different navigation logic in different scenario as you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.