你需要按照這個例子。 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 };
}
}
從我的問題:「在默認情況下在MyMasterDetailPage.xaml.cs在ListView_ItemSelected有一行Detail = new NavigationPage(頁面),它保留了漢堡包菜單,但實際上並不在詳細信息頁面之間導航。**在Android上按下關閉應用程序**「 – Andy
你能分享你的代碼,以便我們看到發生了什麼? – sonicbabbler