簡體   English   中英

Xamarin.Forms - 主/詳細頁面和導航歷史記錄問題

[英]Xamarin.Forms - Master/detail page and navigation history issue

我有一個使用masterdetail頁面在所有頁面顯示菜單的應用程序。 導航在我的應用程序中以兩種方式發生。 一個來自菜單,另一個來自儀表板。 因此,如果我導航到另一個頁面,然后按“返回”按鈕,它將關閉該應用程序。 它不記得導航歷史記錄。 主詳細信息頁面如下:

 public class RootPage : MasterDetailPage
    {
        public RootPage ()
        {
            var menuPage = new MenuPage ();

            menuPage.Menu.ItemSelected += (sender, e) => NavigateTo (e.SelectedItem as MenuItem);

            Master = menuPage;
            Detail = new NavigationPage (new ContractsPage ());
        }

        void NavigateTo (MenuItem menu)
        {
            Page displayPage = (Page)Activator.CreateInstance (menu.TargetType);
            Detail =    new NavigationPage (displayPage);
            IsPresented = false;
        }
    }

所以任何想法如何克服這個問題?

就像@ Sten-Petrov所說:你正在取代細節頁面而不是觸發歷史機制。 要觸發歷史記錄機制,您需要在Detail頁面的Navigation屬性上執行PushAsync(Page)。

在您的示例中,更改NavigateTo:

 void NavigateTo (MenuItem menu)
 {
     Page displayPage = (Page)Activator.CreateInstance (menu.TargetType);
     Detail.Navigation.PushAsync(displayPage);
 }

這不會取代內容,但會打開一個帶有所需后退按鈕功能的新頁面。

如果你想要使用Master-Detail頁面的后退按鈕功能,那么你需要自定義后台堆棧過程,在我看來,這是不值得的。 在這種情況下,只需移動到不同的頁面/導航結構。

這里的問題是你沒有使用導航堆棧來執行頁面的轉換,而是替換你自己頁面上的項目,因此除了導航到你的MasterDetailPage的頁面之外,沒有導航歷史記錄可以“返回”。

您可以通過創建一個繼承MasterDetailPage並初始化菜單的新MenuMasterDetail.cs類來解決此問題,然后創建從您的公共基礎繼承的MenuItem_A_Page.xaml(或.cs),並在您將使用Navigation.PushAsync(...)公共基類中Navigation.PushAsync(...)在頁面之間轉換。

基類:

public class MenuDetailPage: MasterDetailPage{
  public MenuDetailPage(): base(){
    this.Master = BuildMyMenuListHere(); // the menu items will also define navigation targets
  }
}

CS中的子類:

public class FirstDetailWithMenuPage: MenuDetailPage{
  public FirstDetailWithMenuPage()
    : base() // this creates the menu
  {
    this.Detail = new StackLayout{  // change this however you need
      Children = {
        new Label { Text = "This is the first page" },
        new Button { Text= "Ok"},
     }
  }
}

XAML中的子類(以及上面的CS,減去設置Detail的部分):

<local:FirstDetailWithMenuPage namespace:YourNamespace;assembly=YourAssemblyName" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:local="clr-n xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="FirstDetailWithMenuPage">
    <local:FirstDetailWithMenuPage.Detail>
...

同時更新您的App.cs以返回包含您擁有的第一個主/詳細頁面的導航頁面(不是基本頁面):

App.cs:

public static Page GetMainPage ()
{
  return new NavigationPage(new FirstDetailWithMenuPage());
}

我遇到了同樣的問題, Detail.Navigation.PushAsync(itemSelected)使漢堡包菜單消失 ,同時創建另一個子類來保留代碼和性能方面的大量工作。 所以,我決定將自己的堆棧數據類型用於Master詳細信息頁面。 保持跟蹤和代碼但工作正常有點棘手。

使用當前詳細信息頁面在應用程序加載時初始化它,並為選中的每個項目在堆棧頂部推送新頁面。

public partial class MyMasterDetailPage: MasterDetailPage
    {
        private Stack navigationStack = new Stack();
        public MyMasterDetailPage()
        {
            InitializeComponent();
            navigationStack.Push(Detail);
            try
            {
                masterPage.listView.ItemSelected += OnItemSelected;

            }
            catch (Exception exc)
            {

                System.Diagnostics.Debug.WriteLine(exc.Message);
            }

        }

在后面的相同頁面代碼中覆蓋OnBackButtonPressed()

        protected override bool OnBackButtonPressed()
        {
            try
            {
                var lastPage = navigationStack.Pop();
                if (lastPage.Equals(Detail))
                    lastPage = navigationStack.Pop();

                Detail = (Page)lastPage;
                IsPresented = false;

               // to avoid app close when complete pop and new page is push on top of it 
                if (navigationStack.Count == 0) 
                    navigationStack.Push(Detail);
                return true;
            }
            catch (Exception)
            {

                return base.OnBackButtonPressed();
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM