shellxamarinflyout

Xamarin Shell Flyout created from database data


I have app of lists and items in each list. The list and their items are stored in two tables. A list data would be like "Grocery" and the list items like "bananas" etc. You can add lists and items in the app. Right now I have a list content page that lists the list names. Tap one and you go to a content page of the items in that list. This works, but I want to populate the Shell flyout with the list names from a database query in the AppShell.xaml.cs file. I want to do something like this:

                ShellSection shell_section = new ShellSection();

                var lists = await SLOMBListDataStore.GetSLOMBListsAsync(true);
                foreach (var list in lists)
                {
                    shell_section.Title = list.ListName;
                    shell_section.Icon = "tab_feed.png";
                    shell_section.Items.Add(new ShellContent() { Content = new SLOMBListDataPage(new SLOMBListDataViewModel(list.id)) });
                    this.Items.Add(shell_section);
                }

                BindingContext = this;
            

This, by the way, does not work. Creating the entries works manually if I use shell_section1, shell_section2 etc, but then you have to hard code the lists. Is it possible to do what I want, create the flyout from the query data? Thanks


Solution

  • Try to put the initialization code into the for loop like:

    var lists = await SLOMBListDataStore.GetSLOMBListsAsync(true);
    foreach (var list in lists)
    {
        ShellSection shell_section = new ShellSection();
    
        shell_section.Title = list.ListName;
        shell_section.Icon = "tab_feed.png";
        shell_section.Items.Add(new ShellContent() { Content = new SLOMBListDataPage(new SLOMBListDataViewModel(list.id)) });
        this.Items.Add(shell_section);
    }
    

    You need a new instance for each record.