[英]Xamarin Forms Master-Detail Page Menu
I have a MainPage that has a MasterDetailPage I am using it to store the menupage control. 我有一个具有MasterDetailPage的MainPage,我正在用它来存储菜单页控件。 The menu display but I am not able to navigating to the new page.
菜单显示,但我无法导航到新页面。
Do I need to add a MasterDetailPage.Detail to the homePage.xaml?
我是否需要将MasterDetailPage.Detail添加到homePage.xaml?
MenuPage MenuPage
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<StackLayout Orientation="Horizontal" Spacing="10">
<Label VerticalOptions="Center" Text="SideDrawer" />
</StackLayout>
<StackLayout VerticalOptions="FillAndExpand">
<ListView x:Name="ListViewMenu"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Label Text="{Binding Title}" FontSize="20"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</Grid>
public partial class MenuPage : ContentPage
{
MainPage RootPage { get => Application.Current.MainPage as MainPage; }
public NavigationPage Detail { get; private set; }
List<HomeMenuItem> menuItems;
public MenuPage ()
{
InitializeComponent ();
menuItems = new List<HomeMenuItem>
{
new HomeMenuItem {Id = MenuItemType.Home, Title="Home" ,IconSource="Home.png", TargetType = typeof(Pages.HomePage)},
new HomeMenuItem {Id = MenuItemType.Share, Title="Share App" ,IconSource="Home.png" ,TargetType = typeof(Pages.HomePage)},
new HomeMenuItem {Id = MenuItemType.About, Title="About App",IconSource="Home.png",TargetType = typeof(Pages.HomePage) }
};
ListViewMenu.ItemsSource = menuItems;
ListViewMenu.ItemSelected += OnItemSelected;
}
void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as HomeMenuItem;
if (item != null)
{
//This will create instance of the page using the parameterized constructor you defined in each DetailPages
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
}
}
}
You need a page that inherits from MasterDetailPage
. 您需要一个从
MasterDetailPage
继承的MasterDetailPage
。 That will give you a Detail property to set and navigate properly. 这将为您提供一个Detail属性,以正确设置和导航。
I would suggest following this guide to help you out! 我建议您遵循本指南以帮助您!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.