繁体   English   中英

如何重写后退按钮的操作

[英]How to rewrite backbutton's action

在 Page-A 中,我编写了一个命令以使用

App.Current.MainPage.Navigation.PushAsync(new B_Page())

后退按钮动作的原始行为(在页面的左上方)是 go 回到最后一页(页面-A)。 -->Action_A

现在,我想通过按下后退按钮重写将页面更改为新页面(称为 Page-C)的行为。 -->动作_B

使用Xamarin.Forms.Application.Current.MainPage = new NavigationPage(new C_Page());

但我想保留这两种行为,因为我将在不同的场景中使用它们中的每一种。

如何保留这两种行为以及如何让我指定要使用的操作?

让我在场景 A 中使用 Action_A,在场景 B 中使用 Action_B。

感谢您的友好回复。

在此处输入图像描述

您可以使用此覆盖方法管理上述场景

`

    protected override bool OnBackButtonPressed()
    {
        //here you can manage your Navigation.

        return base.OnBackButtonPressed();
    }

`

我们可以重写 ContentPage 的Navigation View 。并根据需要重写后退按钮。

创建一个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>

你只需要创建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>

您可以根据需要在不同的场景中处理不同的导航逻辑。

暂无
暂无

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

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