简体   繁体   English

如何重写后退按钮的操作

[英]How to rewrite backbutton's action

In Page-A, I write a command to direct to Page-B using在 Page-A 中,我编写了一个命令以使用

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).后退按钮动作的原始行为(在页面的左上方)是 go 回到最后一页(页面-A)。 -->Action_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.现在,我想通过按下后退按钮重写将页面更改为新页面(称为 Page-C)的行为。 -->Action_B -->动作_B

using Xamarin.Forms.Application.Current.MainPage = new NavigationPage(new C_Page());使用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.让我在场景 A 中使用 Action_A,在场景 B 中使用 Action_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.我们可以重写 ContentPage 的Navigation View 。并根据需要重写后退按钮。

Create a BaseContentPage创建一个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你只需要创建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.您可以根据需要在不同的场景中处理不同的导航逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM