Binding to Shell.NavBarIsVisible property of AppShell.xaml

dg2k 1,386 Reputation points
2023-05-30T12:15:29.91+00:00

I was trying to bind to Shell.NavBarIsVisible property so as to hide and unhide the navigation bar as desired. Basically, I wanted to hide the navigation bar for a certain page, only leaving the option of device hardware Back Button.

The binding doesn't seem to work. I tried both options of using Binding to ObservableProperty and Binding to a DynamicResource. Simply setting Shell.NavBarIsVisible true or false in XAML works as expected.

Any tips how to get the binding to work?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,906 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,551 Reputation points Microsoft Vendor
    2023-05-31T05:44:27.07+00:00

    Hello,

    Please try the following code and check if it works for you.

    <ContentPage ...
                 xmlns:local ="clr-namespace:XXX"
                 Shell.NavBarIsVisible="{Binding IsHidden}"// It works if IsHidden is true or false when the Page is loaded
                 >
        <ContentPage.BindingContext>
            <local:ShellViewModel></local:ShellViewModel>
        </ContentPage.BindingContext>
    
     private void OnCounterClicked(object sender, EventArgs e)
        {// click button 
            ShellViewModel viewModel = this.BindingContext as ShellViewModel;
            viewModel.IsHidden = !viewModel.IsHidden;
            Shell.SetNavBarIsVisible(this, viewModel.IsHidden);// If you want to change the value in the Page, you have to call SetNavBarIsVisible method
        }
    

    Best Regards, Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 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

0 additional answers

Sort by: Most helpful