maui
maui copied to clipboard
In a ToolbarItems, if an item has no icon but just text, MAUI uses the icon from the previous page in the Navigation
Description
On my first page, I have a ToolBarItems with an item and I assign an icon to that item. On my second page I have another ToolBarItems with an item without icon but just text. When I navigate from my first page to my second page, the toolbar item in this page, instead of having a text, has the same icon from the previous page.
Steps to Reproduce
I've created a quick test to reproduce the issue: TestToolbarItemsIssue.zip
Version with bug
6.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 11
Did you find any workaround?
No response
Relevant log output
No response
verified repro on android with above project.
same here
I have the same issue
same here
This issue still persists. Any update or workaround on that? I tried setting the "IconImageSource" null both from XAML and code. Negative.
same here
Same bug here, I fixed with ugly code : clear the toolbar then add it again 50ms later :
protected override void OnAppearing()
{
base.OnAppearing();
ToolbarFixBug();
}
void ToolbarFixBug()
{
var toolbarItems = this.ToolbarItems?.ToList();
Task.Run(async () =>
{
this.ToolbarItems.Clear();
if (toolbarItems?.Count > 0)
{
await Task.Delay(50);
Dispatcher.Dispatch(() => toolbarItems.ForEach((toolbar) => this.ToolbarItems.Add(toolbar)));
}
});
}
Same here!
Verified this on Visual Studio Enterprise 17.6.0 Preview 7.0. Repro on Android 13.0 with below Project: 7823.zip
For us the hack from @Poppyto caused our app to become unstable on our physical Android testing device.
We employed the same code only without running it inside a Task (and the Dispatcher). No issues so far with this route.
@DP-Technology-LLC reading my code months later, I thinks this.ToolbarItems.Clear(); has to be in the dispatcher. Be careful I tested my code on a very old device, it’s maybe inappropriate for new fast devices.
@Poppyto Ha! You made our testing device blush. :-)
Thought I'd chime in to say that the issue is present for me too in Prism.Maui applications, which uses classic navigation. When I had last looked into it, the issue wasn't present in shell navigation.
same issue
https://github.com/dotnet/maui/assets/42885949/57de51ac-f37f-485a-9a76-9ef6f15985f9
I had the same issue. but made a work-around by making the toolbaritem's priority on second page as 1. Whereas i didn't set any priority on the first page and so far it is working. I know its not a fix but a work-around.
We have noticed some similar undesired behavior regarding SearchHandlers in Views that are in Tabs which are in FlyoutItems on Windows.
ie:
<FlyoutItem Route="FI" Title="FI">
<Tab Title="T" >
<ShellContent
Route="SC"
Title="SC"
ContentTemplate="{DataTemplate views:SCView}" />
... (SearchHandler is in SCView)
When first navigating to SC, no SearchHandler on Windows, navigate away and come back and WALA there it is.
We believe the ToolbarItem issue is now fixed when targeting .net 8. In addition, when you have a ToolbarItem that has an icon and text, the text is no longer hidden on Windows. In .net 7 it used to be located under the icon and the only way to see the text was to click the Secondary ToolbarItems "dots", which would expand the entire Toolbar down. On Windows that text is now to the right of the icon.
We are still seeing the Search Handler issue on Windows so clearly that is unrelated although it feels similar.
@DP-Technology-LLC, It sounds like you are describing a different toolbar issue, did you mean to comment on a different issue?
@Adam-- no this issue. The text issue was something we knew about but never reported it (and honestly forgot about it until we saw it was fixed).
About the SearchHandler issue, we made a comment on Oct 27 in this issue that SearchHandler was showing similar behavior in a certain setup and wanted to make sure nobody assumed that was fixed in .net 8 like the ToolbarItem issue was.
Will most likely create a new issue for SearchHandler, not going to do it at this moment since it seems like a Repro-Repo should be created before reporting the issue.
This is still broken in 8.0.7 and I spent days trying to figure out a workaround. To hopefully save some time for anybody else that is having this issue, the following code should fix it for you:
ToolbarHandler.Mapper.PrependToMapping("ToolbarItems", (h, t) =>
{
int menuItemCount = h.PlatformView.Menu?.Size() ?? 0;
if (menuItemCount > 0 && t is Toolbar toolbar)
{
ToolbarItem[] toolbarItems = toolbar.ToolbarItems.ToArray();
for (int menuItemIndex = 0; menuItemIndex < menuItemCount; menuItemIndex++)
{
IMenuItem menuItem = h.PlatformView.Menu.GetItem(menuItemIndex);
menuItem.SetIcon(null);
}
}
});
I put it in the action that I'm passing into ConfigureMauiHandlers (for Android only). It will also work with ModifyMapping instead of PrependToMapping, if you for some reason need/want to use that.