maui icon indicating copy to clipboard operation
maui copied to clipboard

[android] Identified crash in toolbar extensions when using a custom image source

Open softlion opened this issue 2 months ago • 3 comments

Description

On Android, when using a custom image source as the source for an IconImageSource of a Toolbar item, and that source does not implement GetConstantState, maui crashes:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     xmlns:svg="https://vapolia.eu/svg">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="{x:Static appresource:AppResources.Home_Filter}" 
                     Command="{Binding OpenFilterViewCommand}" 
                     Order="Primary"
                     IconImageSource="{svg:Svg my_icon.svg}" />
    </ContentPage.ToolbarItems>

Steps to Reproduce

The issue is caused by the code in https://github.com/dotnet/maui/blob/main/src/Controls/src/Core/Platform/Android/Extensions/ToolbarExtensions.cs

which ignores the null value returned by baseDrawable.GetConstantState() as not all custom drawable implements that caching feature.

image

Instead, it should not ignore the null value, and provides an alternate implementation when GetConstantState() returns null

Link to public reproduction project repository

No response

Version with bug

8.0.21 SR4.1

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android all versions

Did you find any workaround?

no

Relevant log output

04-30 10:56:46.028 I/DOTNET  (19040): 97|2024-04-30T10:56:46.0278601+00:00|ERROR|1|IImageSource|Unexpected exception in LoadImage. --> System.NullReferenceException: Object reference not set to an instance of an object.
04-30 10:56:46.028 I/DOTNET  (19040):    at Microsoft.Maui.Controls.Platform.ToolbarExtensions.<>c__DisplayClass13_0.<UpdateMenuItemIcon>b__0(IImageSourceServiceResult`1 result) in D:\a\_work\1\s\src\Controls\src\Core\Platform\Android\Extensions\ToolbarExtensions.cs:line 366
04-30 10:56:46.028 I/DOTNET  (19040):    at Microsoft.Maui.ImageSourceExtensions.LoadImageResult(Task`1 task, Action`1 finished) in D:\a\_work\1\s\src\Core\src\ImageSources\ImageSourceExtensions.cs:line 32
04-30 10:56:46.028 I/DOTNET  (19040):    at Microsoft.Maui.TaskExtensions.FireAndForget(Task task, Action`1 errorCallback) in D:\a\_work\1\s\src\Core\src\TaskExtensions.cs:line 36

softlion avatar Apr 30 '24 11:04 softlion

/similarissues

PureWeen avatar May 02 '24 22:05 PureWeen

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar May 02 '24 22:05 github-actions[bot]

The repro is in the comment. Along with the line where the bug is.

softlion avatar May 03 '24 14:05 softlion

@softlion can you attach a full project please?

PureWeen avatar May 07 '24 07:05 PureWeen

Man, the issue is so obvious - I pointed the exact line in the maui source code - that i won't spend 1 more hour on it.

The maui code voluntarily ignores the null return value with an exclamation mark. If it can return null, why ignore it ?

I'm the publisher of a custom component that creates a full custom imagesource with a custom android Drawable.
I already updated my component so it now implements that optional Drawable override, So that issue is worked around.

softlion avatar May 07 '24 08:05 softlion

So that issue is worked around.

Good to hear!

PureWeen avatar May 07 '24 08:05 PureWeen

So that issue is worked around.

Good to hear!

Do you have a financial bonus when you close an issue ?

I would like to understand why you dont add a if( xxxx is not null ) in the source code instead of leaving that bug there and closing that issue ?

softlion avatar May 07 '24 11:05 softlion