I defined some secondary ToolbarItems
, that I want to see (mostly) everywhere, in the AppShell
like this:
<Shell
...
>
<Shell.ToolbarItems>
<ToolbarItem Text="Test1" Order="Secondary" Priority="0" />
<ToolbarItem Text="Test2" Order="Secondary" />
<ToolbarItem Text="Test4" Order="Secondary" />
</Shell.ToolbarItems>
...
</Shell>
But now, there are some pages, mostly at the end of the navigation tree, where I don't want to have these options, because they don't make sense there.
Is there a way, to remove or hide these ToolbarItems just on these individual pages?
Unfortunately, simply calling
protected override void OnAppearing()
{
base.OnAppearing();
Shell.Current.ToolbarItems.Clear();
}
will remove them permanently.
Any ideas to accomplish them only for the current page?
You can create a bindable
ToolBoxItem
,then you can control the visibility through a view model property.I achieved this functionality, you refer to the following code.
1.create
BindableToolbarItem.cs
and add BindablePropertyIsVisible
.2.create a model to save different visible variables for different
ToolbarItem
.3.In
AppShell.xaml.cs
,define an instance forTestVisibleModel
and bind the different variables to theIsVisible
property ofToolbarItem
inAppShell.xaml
.And in
AppShell.xaml
:4.In different pages, you can send message to
AppShell.xaml.cs
to change the value of differentIsVisible
variable.The code of
VisibleChangedMessage.cs
Note:
For how to send message, please check document: Messenger.You need to install nuget
CommunityToolkit.Mvvm
.