locked
Menu in Master Detail Not Active Selected When Tab Menu Clicked RRS feed

  • Question

  • User390692 posted

    I have created Master Detail Page with the tab page as detail. When I clicked the bottom tab menu, the menu in Master Detail doesn't selected as per clicked. Example, I clicked Music Tab at the bottom but menu at master page shows Home menu is active. How to solve this matters? Anyone knows?

    Thursday, July 9, 2020 1:52 AM

All replies

  • User394276 posted

    Can you show the code?

    Thursday, July 9, 2020 4:24 AM
  • User390692 posted

    Here is my codes:

    --- MasterPage.xaml ---

         <MasterDetailPage.Master>
        ...
        <ListView x:Name="navList" RowHeight="50" SeparatorVisibility="None" ItemSelected="NavList_ItemSelected">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>
                                            <StackLayout Orientation="Horizontal" Margin="20,10,0,5" Spacing="10">
                                                <Image Source="{Binding Icon}" />
                                                <Label Text="{Binding Title}" FontSize="Medium" VerticalOptions="Center" TextColor="White" />
                                            </StackLayout>
                                        </ViewCell>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
        ..
        <MasterDetailPage.Detail>
                <NavigationPage>
                    <x:Arguments>
                        <pages:TabPage></pages:TabPage>
                        <!--<pages:HomePage></pages:HomePage>-->
                    </x:Arguments>
                </NavigationPage>
            </MasterDetailPage.Detail>
         </MasterDetailPage>      
    

    --- MasterPage.xaml.cs ---

        public partial class MasterPage : MasterDetailPage
            {
        protected override async void OnAppearing()
                {
                    base.OnAppearing();
                    navList.ItemsSource = GetMasterPageLists();
    
                }
         private void NavList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
                {
                    var item = (MasterPageList)e.SelectedItem;
    
                    if (item.Title == "Setting")
                    {
                        Detail.Navigation.PushAsync(new SettingsPage());
                        IsPresented = false;
                    }
                    else if (item != null)
                    {
                        Application.Current.Properties["MenuName"] = item.Title;
                        Detail = new NavigationPage(new TabPage());
                        IsPresented = false;
                    }
                }
    
                List<MasterPageList> GetMasterPageLists()
                {
                    return new List<MasterPageList>
                    {
                        new MasterPageList() { Title = "Home", Icon = "home.png" },
                        new MasterPageList() { Title = "Music", Icon = "music.png" },
                        new MasterPageList() { Title = "Profile", Icon = "profile.png" },
                        new MasterPageList() { Title = "Log Out", Icon = "logout.png" }
                    };
                }
        }
    

    --- TabPage.xaml.cs ---

        public partial class TabPage : Xamarin.Forms.TabbedPage
            {
        public TabPage ()
        {
                InitializeComponent ();
                    this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);
    
                    if (Xamarin.Forms.Application.Current.Properties.ContainsKey("MenuName"))
                    {
                        var menuName = Xamarin.Forms.Application.Current.Properties["MenuName"].ToString();
    
                        if (menuName == "Home")
                        {
                            this.CurrentPage = Children[0];
                        }
                        else if (menuName == "Music")
                        {
                            this.CurrentPage = Children[1];
                        }
                        else if (menuName == "Profile")
                        {
                            this.CurrentPage = Children[2];
                        }
                    }
    
        }
        }
    
    Thursday, July 9, 2020 5:22 AM
  • User369978 posted

    What makes you think that clicking bottom tab would affect the selection in master page ?

    TabPage is detail of master detail page , all the behavior inside it would never affect master page , they are totally two different navigation ways.


    You could try to send massaging center in TabPage and set Listview.SelectedItem according to the data while receiving the message in Master page .

    Thursday, July 9, 2020 7:47 AM