maui icon indicating copy to clipboard operation
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

Open marcojak opened this issue 2 years ago • 8 comments

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

marcojak avatar Jun 07 '22 16:06 marcojak

verified repro on android with above project. image

kristinx0211 avatar Jun 09 '22 06:06 kristinx0211

same here

bx67212 avatar Aug 31 '22 13:08 bx67212

I have the same issue

Madde88 avatar Sep 27 '22 22:09 Madde88

same here

minglu avatar Nov 03 '22 14:11 minglu

This issue still persists. Any update or workaround on that? I tried setting the "IconImageSource" null both from XAML and code. Negative.

tataelm avatar Nov 23 '22 14:11 tataelm

same here

duepuntotre avatar Dec 18 '22 15:12 duepuntotre

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)));
            }
	});
}

Poppyto avatar Jan 18 '23 13:01 Poppyto

Same here!

nicop85 avatar Mar 01 '23 13:03 nicop85

Verified this on Visual Studio Enterprise 17.6.0 Preview 7.0. Repro on Android 13.0 with below Project: 7823.zip

image

XamlTest avatar May 16 '23 06:05 XamlTest

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 avatar Jul 28 '23 03:07 DP-Technology-LLC

@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 avatar Jul 28 '23 18:07 Poppyto

@Poppyto Ha! You made our testing device blush. :-)

DP-Technology-LLC avatar Jul 28 '23 19:07 DP-Technology-LLC

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.

Adam-- avatar Aug 16 '23 16:08 Adam--

same issue

https://github.com/dotnet/maui/assets/42885949/57de51ac-f37f-485a-9a76-9ef6f15985f9

pulmuone avatar Aug 17 '23 14:08 pulmuone

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. image

TechnologyCell-TC avatar Sep 16 '23 20:09 TechnologyCell-TC

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.

DP-Technology-LLC avatar Oct 28 '23 03:10 DP-Technology-LLC

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 avatar Nov 16 '23 15:11 DP-Technology-LLC

@DP-Technology-LLC, It sounds like you are describing a different toolbar issue, did you mean to comment on a different issue?

Adam-- avatar Nov 16 '23 16:11 Adam--

@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.

DP-Technology-LLC avatar Nov 16 '23 17:11 DP-Technology-LLC

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.

bzd3y avatar Mar 08 '24 01:03 bzd3y