在 Xamarin 中关闭 Popup 时如何返回到 TabbedPage 的上一页

How to go back to previous page of TabbedPage when closing Popup in Xamarin

我正在了解有关 TabbedPage 的更多信息。我有一个问题希望有人能帮助我:

我有 TabbedPage 页面(Page1、Page2、Page3、Page4、Page5)。当我 select TabbedPage 的第 5 页时,它显示 Popup。当我关闭 Popup 时,它如何回到我触摸的 TabbedPage 的上一个页面。

例如:我在select页面2 TabbedPage,接下来我选择Page5 TabbedPage,它显示Popup,我关闭Popup,它会回到TabbedPage的selectPage2,类似:我正在 selecting Page3 TabbedPage,接下来我选择 Page5 TabbedPage ,它显示 Popup,我关闭 Popup,它将返回到 select Page3 TabbedPage,.....

请注意,它将返回到之前 select 编辑的 TabbedPage。

MainView.xaml

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            BackgroundColor="#fff".....>
    <!--Pages can be added as references or inline-->
    <views:Page1 Title="Page 1" IconImageSource="homeicon" BackgroundColor="#f7f7f7"/>
    <views:Page2 Title="Page 2" IconImageSource="manageorder" BackgroundColor="#fff"/>
    <views:Page3 Title="Page 3" IconImageSource="feeds" BackgroundColor="#fff"/>
    <views:Page4 Title="Page 4" IconImageSource="moneys" BackgroundColor="#fff"/>
    <views:Page5 Title="Page 5" IconImageSource="accounticon" BackgroundColor="#fff"/>
</TabbedPage>

MainView.xaml.cs

private async void PopupAlert()
{
    await PopupNavigation.Instance.PushAsync(new PopupAlertPage());
}

protected override void OnCurrentPageChanged()
{
    base.OnCurrentPageChanged();
    if (CurrentPage is Page5)
    {
        PopupAlert();
    }
}

PopupAlertPage.xaml.cs

private void close_Tapped(object sender, EventArgs e)
{

    //App.Current.MainPage = new NavigationPage(new MainView(0));
    PopupNavigation.Instance.PopAllAsync();
    Application.Current.MainPage.Navigation.PopAsync();
}

寻求帮助。非常感谢

更新1

MainView.xaml.cs

public partial class MainView
{
    public MainView(int index)
    {
        NavigationPage.SetHasNavigationBar(this, false);
        InitializeComponent();
        On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
        //CurrentPage = Children[0];
        
        SetPage(index);
    }
    void SetPage(int index)
    {
        CurrentPage = Children[index];
    }
    private async void PopupAlertLogin()
    {
        await PopupNavigation.Instance.PushAsync(new PopupAlertLogin());
    }

    protected override void OnCurrentPageChanged()
    {
        base.OnCurrentPageChanged();
        if (CurrentPage is Page5)
        {
            PopupAlertLogin();
        }
    }
}

PopupAlertPage.xaml.cs

private void close_Tapped(object sender, EventArgs e)
{
    PopupNavigation.Instance.PopAllAsync();

    var tab = (TabbedPage)Application.Current.MainPage;
    tab.CurrentPage = tab.Children[0];

    //var mainPage = this.Parent as TabbedPage;
    //mainPage.CurrentPage = mainPage.Children[4];
}

错误:

虽然我不支持你的逻辑,但你可以通过这种方式实现。

在您的 MainView Class 中,声明一个 属性 OldPageIndex 并添加此函数。

 public static string OldPageIndex { get; set; }

 protected override async void OnCurrentPageChanged()
        {
            
            base.OnCurrentPageChanged();
            
            if (this.CurrentPage is Page5)
            {
              //await Navigation.PushAsync(new popup());
               // Navigate to any page you want . 
               //PopupAlertLogin
            }
            else
            {
                OldPageIndex = this.CurrentPage.Title;
            }
        }

您在页面上导航 PopupAlertLogin 到关闭功能。

private void close_Tapped(object sender, EventArgs e)
        {
            var tab = (App.Current.MainPage as NavigationPage).RootPage as TabbedPage;
            tab.CurrentPage = tab.Children.FirstOrDefault(c => c.Title == MainView.OldPageIndex);
            PopupNavigation.Instance.PopAllAsync();
        }

您可以使用 Children.IndexOf(CurrentPage) 获取 TabbedPage 的标签索引。然后使用 MessageCenter 导航到具有特定索引的页面。

TabbedPage1:

public partial class TabbedPage1 : Xamarin.Forms.TabbedPage
{
    public static int index { get; set; }     
    public TabbedPage1()
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);
        InitializeComponent();
        On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
        //CurrentPage = Children[0];
        MessagingCenter.Subscribe<Object, int>(this, "Navigation", (arg, idx) =>
        {
            // idx: the index of pages in tabbed that you want to navigate
            CurrentPage = this.Children[idx];

        });
    }
    private async void PopupAlert()
    {
        await PopupNavigation.Instance.PushAsync(new PopupAlertLogin());

    }
    protected override void OnCurrentPageChanged()
    {
        base.OnCurrentPageChanged();

        if (CurrentPage is Page5)
        {
            PopupAlert();
        }
        else
        {
            index = Children.IndexOf(CurrentPage);

        }
    }
}

PopupAlertLogin:

    private void close_Tapped(object sender, EventArgs e)
    {                  
        MessagingCenter.Send<object, int>(this, "Navigation", TabbedPage1.index);// if you want to navigation to the specific page .
        PopupNavigation.Instance.PopAllAsync();
    }