简体   繁体   English

Xamarin MasterDetailPage按钮

[英]Xamarin MasterDetailPage button

How would I reference the Master Detail Page (MenuNavigation) by a clickable button inside of Homepage.xaml.cs and Homepage.xaml? 我如何通过Homepage.xaml.cs和Homepage.xaml中的可单击按钮引用“母版详细信息页面”(MenuNavigation)?

I want the side menu to appear whenever I click the "menu" button. 我希望每次单击“菜单”按钮时都显示侧面菜单。 (see below) I have tried MasterDetail.isPresentedProperty inside the clicked button event handler in Homepage.xaml.cs and cannot figure it out. (请参阅下文)我已经在Homepage.xaml.cs中单击的按钮事件处理程序中尝试了MasterDetail.isPresentedProperty,但无法弄清楚。

Please help! 请帮忙!

在此处输入图片说明 在此处输入图片说明

MenuNavigation.xaml: MenuNavigation.xaml:

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="App2.MenuNavigation"
              xmlns:local="clr-namespace:App2">


<!--this is the Menu Hamburger sidebar -->
<MasterDetailPage.Master>
    <ContentPage Title="Menu" Padding="20" BackgroundColor="#4B1388" >
        <StackLayout Orientation="Vertical">
            <Label Text="Menu" TextColor="White" FontSize="Large" FontAttributes="Bold"  HorizontalOptions="Center"/>

            <Button Text="Home"
                    TextColor="White" BackgroundColor="#4B1388" 
                    Clicked="Button_Clicked_Home"/> <!-- event handler points to MenuNavigation C# file when button is clicked-->
            <Button Text="About" 
                    TextColor="White" BackgroundColor="#4B1388" 
                    Clicked="Button_Clicked_About"/> 
            <Button Text="Help" 
                    TextColor="White" BackgroundColor="#4B1388" 
                    Clicked="Button_Clicked_Help"/>
        </StackLayout>
    </ContentPage>
</MasterDetailPage.Master>

<!--links to the detail pages from the hamburger sidebar -->
<MasterDetailPage.Detail>

</MasterDetailPage.Detail>

MenuNavigation.xaml.cs: MenuNavigation.xaml.cs:

namespace App2
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MenuNavigation : MasterDetailPage
{
    public MenuNavigation()
    {
        InitializeComponent();


        //this is for no nav bar at top
        var Homepage = new Homepage();
        NavigationPage.SetHasNavigationBar(Homepage, false);
        Detail = new NavigationPage(Homepage);
        IsPresented = false; //this will make the side menu disappear when you select a page


    }

    private void Button_Clicked_Home(object sender, EventArgs e)
    {
        var Homepage = new Homepage();
        NavigationPage.SetHasNavigationBar(Homepage, false);
        Detail = new NavigationPage(Homepage);
        IsPresented = false; //this will make the side menu disappear when you select a page
    }

    private void Button_Clicked_About(object sender, EventArgs e)
    {
        Detail = new NavigationPage(new About());
        IsPresented = false;
    }

    private void Button_Clicked_Help(object sender, EventArgs e)
    {
        Detail = new NavigationPage(new Help());
        IsPresented = false;
    }
}

} }

Homepage.xaml: Homepage.xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="App2.Homepage"
         Title="Homepage">

            <Button Text="Menu" Clicked="MasterDetailButton_Pressed"/>
</ContentPage>

Hompage.xaml.cs: Hompage.xaml.cs:

namespace App2
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Homepage : ContentPage
    {

        public Homepage()
    {
        InitializeComponent();

        VideoListView.ItemsSource = Videos; //allows for binding
    }
    private void MasterDetailButton_Pressed(object sender, EventArgs e)
        {
            MasterDetailPage.IsPresentedProperty.Equals(true);
            //open the master detail page when button is clicked.
            //MasterDetailPage.IsPresentedProperty.Equals(true);
            //MenuNavigation.IsPresentedProperty.Equals(true);
        }
    }
}

You should get a reference of your MasterDetailPage and change the IsPresented property of the reference. 您应该获得MasterDetailPage的引用,并更改引用的IsPresented属性。 As the MasterDetailPage has to be the root of the app, something like this should work: (App.Current.MainPage as MasterDetailPage).IsPresented = true; 由于MasterDetailPage必须是应用程序的根目录,因此应该可以执行以下操作: (App.Current.MainPage as MasterDetailPage).IsPresented = true; .

Hope it helps! 希望能帮助到你! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM