簡體   English   中英

Xamarin表單主從頁面菜單

[英]Xamarin Forms Master-Detail Page Menu

我有一個具有MasterDetailPage的MainPage,我正在用它來存儲菜單頁控件。 菜單顯示,但我無法導航到新頁面。

我是否需要將MasterDetailPage.Detail添加到homePage.xaml?

菜單顏色

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));
        }
    }
}

您需要一個從MasterDetailPage繼承的MasterDetailPage 這將為您提供一個Detail屬性,以正確設置和導航。

我建議您遵循本指南以幫助您!

Xamarin.Forms主從頁面

暫無
暫無

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

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