Are usages of MAUI TabBar Tab menu items and Toolbar menu items mutually exclusive?

dg2k 1,416 Reputation points
2023-04-22T22:09:47.5966667+00:00

I am getting strange behaviour of AppShell based navigation when using both AppShell TabBar Tab items (at the page bottom) and Toolbar items (at the page top toolbar area) for Android.

The problem shows up when navigating from one page to another. The Toolbar icons of a page navigated from appears in a page that is navigated to. It means the image binding is not updated and it requires a bit of back and forth for the icons to appear as expected. I didn't have this issue with Xamarin and only now when migrating to MAUI.

Am I missing some key settings, say for AppShell, or TabBar menu items and Toolbar menu items do not mix?

I thought there are excellent use-cases where app menu items are at the bottom (as TabBar implementation) and a few toolbar items can be used for something else at the top.

In AppShell.xaml

<TabBar
	Title="My Test Page"
	FlyoutIcon="{StaticResource MenuTestIcon}">
<Tab
	Title="tab1"
	Icon="{Binding MenuTab1ImageSource}">
	<ShellContent ContentTemplate="{DataTemplate views:MainPage}"/>
</Tab>

<Tab
	Title="tab2"
	Icon="{Binding MenuTab2ImageSource}">
	<ShellContent ContentTemplate="{DataTemplate views:MainPage}" />
</Tab>

</TabBar>

In MainPage.xaml

<ContentPage.ToolbarItems>

<ToolbarItem
	Command="{Binding ToolbarButtonTappedCommand}"
	CommandParameter="reminder"
	IconImageSource="reminder.png"
	Text="reminder" />

<ToolbarItem
	Command="{Binding ToolbarButtonTappedCommand}"
	CommandParameter="notepad"
	IconImageSource="notepad.png"
	Text="notepad" />

</ContentPage.ToolbarItems>
Developer technologies | .NET | .NET MAUI
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-04-28T07:15:32.1+00:00

    Hello,

    After investigation, it was found that icons did exhibit behavior that did not update as expected when using data binding in the Shell.

    Therefore, you could refer to the following steps to complete this feature using Dynamic styles.

    Step 1. Add string resources in App.xaml:

    <ResourceDictionary>
                <x:String x:Key="Tab1IconSource">dotnet_bot.png</x:String>
                <x:String x:Key="Tab2IconSource">test.png</x:String>
    </ResourceDictionary> 
    

    Step 2. Set up dynamic resources for Icons in your shell:

    <Tab 
        Title="tab1"
        Icon="{DynamicResource Tab1IconSource}">
        <ShellContent ContentTemplate="{DataTemplate local:MainPage}"/>
    </Tab>
    
    <Tab
        Title="tab2"
        Icon="{DynamicResource Tab2IconSource}">
        <ShellContent ContentTemplate="{DataTemplate local:MainPage}" />
    </Tab>
    

    Step 3. You need to change the value of the dynamic resource when you need to change the icon. In this example, I swapped the icons for two tabs.

    var res = App.Current.Resources["Tab1IconSource"]; 
    App.Current.Resources["Tab1IconSource"] = App.Current.Resources["Tab2IconSource"];
    App.Current.Resources["Tab2IconSource"] = res;
    

    Best Regards,

    Alec Liu.


    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Alexander Petree 150 Reputation points
    2023-04-23T01:38:16.6866667+00:00

    <ContentPage.ToolbarItems>


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.