2017-07-06 90 views
1

我正在嘗試使用Xamarin.Forms MasterDetail頁面來實現漢堡菜單導航。我遇到的問題是,根據我如何更改詳細信息頁面,漢堡包菜單消失,但後退功能正常,或漢堡包停留,但我們無法瀏覽詳細信息頁面(硬件/屏幕背面按鈕將用戶返回到設備的主屏幕)。掌握詳細導航?

該問題很容易使用最新的XF(2.3.4.247)進行復制;在Visual Studio中創建一個新的Cross Platform App (Xamarin)。確保所有的Nu​​get包都是最新的。然後到共享項目,添加一個名爲MyMasterPageMasterDetail頁面。

默認情況下MyMasterDetailPage.xaml.csListView_ItemSelected有一行Detail = new NavigationPage(page)它保留漢堡菜單,但實際上不會在詳細信息頁面之間導航。在Android上按下關閉應用程序。

如果更改行Detail.Navigation.PushAsync(page, true)後退按鈕的作品,你可以通過所有先前打開的子項目導航,但漢堡圖標與另一個back按鈕替換(除了一個設備正常顯示)。

如何讓漢堡包菜單保留,以便用戶可以在所有頁面上訪問它,同時仍然允許用戶返回到之前的詳細信息頁面?

回答

1

你需要按照這個例子。 https://developer.xamarin.com/samples/xamarin-forms/Navigation/MasterDetailPage

在我的App.xaml.cs中我將它重定向到Menu頁面。

MainPage = new MainMenu(); 

接下來,我創建了MainMenu的觀點這是一個MasterDetailPage

<MasterDetailPage.Master> 

    <ContentPage 
      Icon="hamburger_menu.png" 
      Title="MyTitle" 
      BackgroundColor="#29632A" 
      > 

     <!--Menu Title background color--> 

     <!--Slide out Menu--> 
     <StackLayout VerticalOptions="FillAndExpand" > 

      <!--Menu Header Layout--> 

      <Label 
       Text="MyTitle" 
       TextColor="White" 
       FontSize="22" 
       VerticalOptions="Center" 
       HorizontalOptions="Center" 
       Margin="0, -10, 0, 5" /> 

      <ListView 
        x:Name="MenuListView" 
        ItemsSource="{Binding MainMenuItems}" 
        ItemSelected="MainMenuItem_Selected" 
        VerticalOptions="FillAndExpand" 
        SeparatorVisibility="None" 
        BackgroundColor="#f5f5f5"> 
       <!--Menu background color--> 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <ViewCell> 
          <ViewCell.View> 
           <StackLayout Orientation="Horizontal" Padding="10,0,0,0"> 
            <!--Menu layout--> 

            <Label Text="{Binding Title}" FontSize="18" VerticalTextAlignment="Center"/> 

           </StackLayout> 
          </ViewCell.View> 
         </ViewCell> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 
     </StackLayout> 

    </ContentPage> 
</MasterDetailPage.Master> 

的MainMenu.xaml.cs

public partial class MainMenu : MasterDetailPage 
{ 
    public List<MainMenuItem> MainMenuItems { get; set; } 

    public MainMenu() 
    { 
     BindingContext = this; 

     // Build the Menu 
     MainMenuItems = new List<MainMenuItem>() 
     { 
      new MainMenuItem() { Title = "Menu1", Icon = "menu1.png", IconSize = 18, TargetType = typeof(Menu1) }, 
      new MainMenuItem() { Title = "Menu2", Icon = "menu2.png", IconSize = 18, TargetType = typeof(Menu2) } 
     }; 

     // Set the default page, this is the "home" page. 
     ChangeDetail(new Menu1()); 
     InitializeComponent(); 

    } 

    // When a MenuItem is selected. 
    public void MainMenuItem_Selected(object sender, SelectedItemChangedEventArgs e) 
    { 
     var item = e.SelectedItem as MainMenuItem; 
     if (item != null) 
     { 
      if (item.Title.Equals("Menu1")) 
      { 
       ChangeDetail(new Menu1()); 
      } 
      else if (item.Title.Equals("Menu2")) 
      { 
       ChangeDetail(new Menu2()); 
      } 

      MenuListView.SelectedItem = null; 
      IsPresented = false; 
     } 
    } 

    public void ChangeDetail(Page page) 
    { 
     var navigationPage = Detail as NavigationPage; 
     if (navigationPage != null) 
     { 
      navigationPage.PushAsync(page); 
      return; 
     } 
     Detail = new NavigationPage(page) { BarBackgroundColor = Color.FromHex("#FF0000"), BarTextColor = Color.White }; 
    } 
} 
+0

從我的問題:「在默認情況下在MyMasterDetailPage.xaml.cs在ListView_ItemSelected有一行Detail = new NavigationPage(頁面),它保留了漢堡包菜單,但實際上並不在詳細信息頁面之間導航。**在Android上按下關閉應用程序**「 – Andy

+0

你能分享你的代碼,以便我們看到發生了什麼? – sonicbabbler