Search code examples
c#collectionsmauiflyout

(MAUI) Access programmatically to Shell.FlyoutItems


I'm try to access from code to my FlyoutItems

    <classes:BCTab Title="Home"  FlyoutIcon="home24.png" Tag="DASHBOARD">
        <ShellContent ContentTemplate="{DataTemplate views:DashboardPage}" />
    </classes:BCTab>
    <classes:BCTab Title="Servizi"  FlyoutIcon="camera24.png" Tag="SERVICES" >
        <ShellContent ContentTemplate="{DataTemplate views:ServicesPage}" />
    </classes:BCTab>
    <classes:BCTab Title="Change Password"  FlyoutIcon="password24.png" Tag="CHGPASSWOR">
        <ShellContent ContentTemplate="{DataTemplate views:ChangePasswordPage}" />
    </classes:BCTab>
</FlyoutItem>`

BCTab is a derived class from Tab with some properties added

in c# I wrote

 foreach (var item in this.FlyoutItems)
 {
   // now here???   
 }

I wanna show or hide the tab element.. permission cames from a REST service that gives me a code (one of the added properties in a List < string >).. it would be simple if I can access to a BCTab element via "item.controls" or "item.childelements" (or somewhat similiar) but I'm unable to understand how to do and all my searches are vain.

Any suggestion?

In debug look at item class and properties, searched on google and MS documentation


Solution

  • I was able to solve this. It ended up being easier than I believed.

    foreach (IEnumerable item in this.FlyoutItems) 
    {
        foreach (BCTab tab in item.ToList<BCTab>()) 
        { 
            Console.WriteLine(tab.Tag); 
        }
    } 
    

    Flyoutitems is an IEnumerable of objects (the same container in XAML between his marking tags). In my specific case, I know they are all of BCTab (the class I derived from Tab), so I use the ToList <BCTab>() extension.

    In a more generic way, it's possibile to use ToList<object>() or ToList<View>() and thus verifying the correct type (usually Tab). With a simple cast of the object, it's then possible to work on the object.