Pooja Priya

Pooja Priya

  • NA
  • 5
  • 7.5k

Create Hamburger menu with dynamic menu list in xamarin form

Jul 18 2017 9:28 AM
Hi all,
 
I have used master detail page to create an hamburger Menu. I would like to have a menu with 3 items initially, once user logged in, the menu should be changed to 4 items.
 
Initally
 
once logged in
 
 
Mainpage.axml
  1. <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"  
  2. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  3. xmlns:local="clr-namespace:xxx"  
  4. x:Class="xxx.MainPage">  
  5. <MasterDetailPage.Master>  
  6. <local:MasterPage x:Name="MasterPage"></local:MasterPage>  
  7. </MasterDetailPage.Master>  
  8. <MasterDetailPage.Detail>  
  9. <NavigationPage>  
  10. <x:Arguments>  
  11. <local:Pages.LoginPage></local:Pages.LoginPage>  
  12. </x:Arguments>  
  13. </NavigationPage>  
  14. </MasterDetailPage.Detail>  
  15. </MasterDetailPage>   
Main Page.axml.cs
  1. public partial class MainPage : MasterDetailPage  
  2. {  
  3. public MainPage()  
  4. {  
  5. InitializeComponent();  
  6. MasterBehavior = MasterBehavior.Popover;//new  
  7. MasterPage.listView.ItemSelected += OnItemSelected;  
  8. Master = MasterPage;  
  9. NavigationPage page = new NavigationPage(new Pages.LoginPage());  
  10. page.BarBackgroundColor = Color.FromHex("#29B6F6");  
  11. Detail = page;  
  12. }  
  13. private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)  
  14. {  
  15. var item = e.SelectedItem as ClassFiles.MasterPageItem;  
  16. if (item!=null)  
  17. {  
  18. Page displayPage = (Page)Activator.CreateInstance(item.TargetType);  
  19. NavigationPage page = new NavigationPage(displayPage);  
  20. page.BarBackgroundColor = Color.FromHex("#29B6F6");  
  21. Detail = page;  
  22. //Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));  
  23. MasterPage.listView.SelectedItem = null;  
  24. IsPresented = false;  
  25. }  
  26. }  
  27. }  
Master Page.axml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4. x:Class="xxx.MasterPage"  
  5. Padding="0,50,0,0"  
  6. Title="Titles">  
  7. <ContentPage.Content>  
  8. <StackLayout VerticalOptions="FillAndExpand">  
  9. <ListView x:Name="lstScreens" VerticalOptions="FillAndExpand" SeparatorVisibility="None">  
  10. <ListView.ItemTemplate>  
  11. <DataTemplate>  
  12. <TextCell Text="{Binding Title}"></TextCell>  
  13. </DataTemplate>  
  14. </ListView.ItemTemplate>  
  15. </ListView>  
  16. </StackLayout>  
  17. </ContentPage.Content>  
  18. </ContentPage>  
Master Page.axml
  1. public partial class MasterPage : ContentPage  
  2. {  
  3. public ListView listView { get { return lstScreens; } }  
  4. public MasterPage()  
  5. {  
  6. InitializeComponent();  
  7. var masterpageItem = new List<ClassFiles.MasterPageItem>();  
  8. masterpageItem.Add(new ClassFiles.MasterPageItem  
  9. {  
  10. Title="Login",  
  11. TargetType = typeof(Pages.LoginPage)  
  12. });  
  13. masterpageItem.Add(new ClassFiles.MasterPageItem  
  14. {  
  15. Title = "Sign Up",  
  16. TargetType = typeof(Pages.SignUpPage)  
  17. });  
  18. listView.ItemsSource = masterpageItem;  
  19. }  
  20. }  
Login.axml.cs
  1. async void Login_Procedure(object sender, EventArgs e)  
  2. {  
  3. tbl_Login objLogin = new tbl_Login();  
  4. objLogin.UserName = EntryUsername.Text;  
  5. objLogin.Password = EntryPassword.Text;  
  6. if (objLogin.checkInformation())  
  7. {  
  8. if (data.GetLoginUserDetails(objLogin))  
  9. {  
  10. Navigation.InsertPageBefore(new Pages.HomePage(), this);//Navigate to Home page in master details relation  
  11. await Navigation.PopToRootAsync();  
  12. }  
  13. else  
  14. {  
  15. Error_label.IsVisible = true;  
  16. Error_label.Text = "Please Enter valid Credentials";  
  17. //await DisplayAlert("Login", "Please Enter valid Credentials", "OK");  
  18. }  
  19. }

Answers (1)