选项卡页面OnBackButtonPressed在Xamarin表单中

时间:2018-06-01 09:05:16

标签: xamarin xamarin.forms back-button

如何在xamarin表单中单击后退按钮时在选项卡页面之间导航

1 个答案:

答案 0 :(得分:0)

试试这个你需要覆盖OnBackButtonPressed事件并保持堆栈

通常,通过单击选项卡本身来完成选项卡页面中的导航。

protected Stack<Page> TabStack { get; private set; } = new Stack<Page>();

protected override void OnCurrentPageChanged()
{

    // current page
    var page = CurrentPage;
    if (page != null)
    {
        // Push the page onto the stack
        TabStack.Push(page);
    }

    base.OnCurrentPageChanged();            
}

protected override bool OnBackButtonPressed()
{

    // Go to previous page in the stack. First, 
    //pop off the top page since this represents the
    // current page we are on.
    if (TabStack.Any())
    {
        TabStack.Pop();
    }

    // Check if we have any pages left
    if (TabStack.Any())
    {
        // Pop off the next page and Launch it
        CurrentPage = TabStack.Pop();
        // Return true to indicate we handled this scenario
        return true;
    }

    // We don't have any more pages in the stack so do the default 
   //funcationlity
    return base.OnBackButtonPressed();
}