2017-06-05 4 views
1

Wie würde ich die Master-Detailseite (MenuNavigation) über eine anklickbare Schaltfläche innerhalb von Homepage.xaml.cs und Homepage.xaml referenzieren?Xamarin MasterDetailPage-Schaltfläche

Ich möchte, dass das Seitenmenü erscheint, wenn ich auf den "Menü" -Button klicke. (siehe unten) Ich habe versucht, MasterDetail.isPresentedProperty innerhalb des Ereignishandlers clicked button in Homepage.xaml.cs und kann es nicht herausfinden.

Bitte helfen!

enter image description hereenter image description here

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:

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:

<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:

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

Antwort

2

Sie sollten einen Verweis Ihrer MasterDetailPage erhalten und die IsPresented Eigenschaft der Referenz ändern. Da die MasterDetailPage der Stamm der App sein muss, sollte so etwas funktionieren: (App.Current.MainPage as MasterDetailPage).IsPresented = true;.

Hoffe es hilft! :)