Changing the property of a tab item from AppShell in another page?

Ian C 1 Reputation point
2021-02-20T18:21:38.067+00:00

Working on Xamarin.Forms app with a Tab at the bottom, defined in the AppShell.xaml file

I want to be able from other pages (page1.xaml, page2.xaml) to be able to enable/disable certain tab items.

For each of the tabs in AppShell I have x:Name="myTabPage1", x:Name="myTabPage2", and so on

I want to be able to do myTabPage1.IsEnabled=false from inside another page, eg page2.xaml.cs

But I can't work out how to reference something in the AppShell.

Any ideas?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-02-22T08:13:17.23+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    If you want to change one ShellSection's IsEnable status, I suggest you can use MessageCenter to do this.

    My ShellItems:

     <ShellItem>  
        <ShellSection  
            x:Name="itempage"  
            Title="Browse"  
            Icon="tab_feed.png">  
            <ShellContent>  
                <local:ItemsPage />  
            </ShellContent>  
        </ShellSection>  
        <ShellSection  
            x:Name="aboutpage"  
            Title="About"  
            Icon="tab_about.png"  
            IsEnabled="False">  
            <ShellContent>  
                <local:AboutPage />  
            </ShellContent>  
        </ShellSection>  
    </ShellItem>  
    

    And subscribe to a message in AppShell.cs:

     public AppShell()  
        {  
            InitializeComponent();  
      
            MessagingCenter.Subscribe<AppShell>(this, "Hi", (sender) =>  
            {  
                aboutpage.IsEnabled = true;  
            });  
        }  
    

    Sending one message in HandlingPage Contentpage:

    private void btn1_Clicked(object sender, EventArgs e)  
        {  
            MessagingCenter.Send<AppShell>(new AppShell(),"Hi");  
        }  
    

    For more about MessageCenter, you can check : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center
    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments