Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Maui.Controls.Shapes.PathGeometryConverter does not load Path Data from Resource files in Release mode, but works fine in Debug mode #20552

Open
hansmbakker opened this issue Feb 13, 2024 · 8 comments
Labels
area-xaml XAML, CSS, Triggers, Behaviors platform/android 🤖 platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@hansmbakker
Copy link

hansmbakker commented Feb 13, 2024

Description

I would like to store my vector art for Paths in a reusable way in the resources. I found the idea to do that by putting the Path Data as strings in in a ResourceDictionary, because it is not possible to put items of type PathGeometry in there - I got this idea from https://stackoverflow.com/questions/41020372/pathgeometry-in-resourcedictionary.

This approach does work in Debug mode - I can load Path Data from a StaticResource of type string.
When using the Release configuration, the Path is not shown.

It seems that the PathGeometryConverter does not work well with a string loaded from a ResourceDictionary.

Debug

image

Release

image

Steps to Reproduce

  1. Create new MAUI App project
  2. Add resources XAML file and include it in App.xaml
  3. Add the string you would normally put in Path Data as a resource in the resources XAML file
  4. Add the Path to your XAML page like
    <Path Data="{StaticResource NameOfYourPathDataResourceString}"
       Fill="Black"
       WidthRequest="24"
       HeightRequest="24" />
  5. Run the app in Debug mode and in Release mode. The Path shows in Debug mode but not in Release mode.

⚠️This happens on physical Android phones, Windows (WinAppSDK) and Windows Subsystem for Android. I do not have the equipment to test on iOS/macOS.

Link to public reproduction project repository

https://github.com/hansmbakker/path-data-resources-bugrepro

Version with bug

Initally reported with version 8.0.6
Still an issue with version 8.0.70

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

Two options:

  1. Put the Data string verbatim in the Path object (avoid using StaticResources) but that defeats the purpose.
  2. instead of a resources XAML file, create a CustomIconData class with a static property like so:
    public static Geometry MyIconGeometry = new PathGeometryConverter().ConvertFrom("<Path Data string goes here>") as Geometry;
    and reference it like so:
    <Path Data="{x:Static styles:CustomIconData.MyIconGeometry}"

As for option 2: this only works if you use data type Geometry. Using type string and hoping to rely on the PathGeometryConverter implicitly does not work..

Relevant log output

[0:] Microsoft.Maui.Controls.BindableObject: Warning: Cannot convert M7.04539 <REDACTED STRING CONTENTS OF THE RESOURCE ITEM> 11.8492V14.9454Z to type 'Microsoft.Maui.Controls.Shapes.Geometry'
@hansmbakker hansmbakker added the t/bug Something isn't working label Feb 13, 2024
@hansmbakker
Copy link
Author

hansmbakker commented Feb 13, 2024

@PieEatingNinjas had the nice idea to create a workaround using an IValueConverter:

public class PathGeometryFromStringConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
        => new PathGeometryConverter().ConvertFromInvariantString(value?.ToString() ?? string.Empty) as Geometry;

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
        => throw new NotImplementedException();
}

This works with a string resource loaded from XAML:

<Path Aspect="Uniform"
      HeightRequest="100"
      WidthRequest="100"
      Data="{Binding Source={StaticResource MyIconGeometry}, 
      Converter={StaticResource converter},
      Mode=OneTime}" />

@jsuarezruiz jsuarezruiz added the area-xaml XAML, CSS, Triggers, Behaviors label Feb 13, 2024
@PieEatingNinjas
Copy link
Contributor

I've been looking into this together with @hansmbakker . It's something we can easily work around, but the real big problem here IHMO, is the fact that the behavior changes depending whether you're in Debug or not. Things like this can leave (new) developers completely stumbled: what once ran perfectly fine on their machine, is broken once it is built as release. This is not a good developer experience and could completely overshadow the overall .NET MAUI experience.

@hansmbakker
Copy link
Author

I was going through issues related to trimming (given that that is one of the differences between debug / release) and saw it is a development goal for MAUI in .NET 9

Could it be that this issue of the string -> Geometry conversion is related to #19922?

@hansmbakker
Copy link
Author

@samhouts I guess it is a generic MAUI issue, not limited to android/windows - I just could not test it on other platforms.

@XamlTest XamlTest added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Mar 20, 2024
@XamlTest
Copy link
Collaborator

Verified this on VS 17.10.0 Preview 2.0(8.0.7). Repro on Windows 11, Android 14.0-API34, iOS 17.2 and MacCatalyst with below Project:
path-data-resources-bugrepro.zip

@symbiogenesis
Copy link
Contributor

symbiogenesis commented Jun 12, 2024

Is this still an issue with .NET 8 SR6 ?

@samhouts samhouts removed s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 3, 2024
@hansmbakker
Copy link
Author

hansmbakker commented Jul 9, 2024

@symbiogenesis @samhouts @XamlTest unfortunately this is still an issue.

Tested with .NET MAUI 8.0.70 on Windows.

Please checkout the reproduction repository and build in Release mode. You'll see that the image does not show up, while it does show up in Debug mode.

@hansmbakker
Copy link
Author

@symbiogenesis @samhouts @XamlTest I updated the reproduction repository to MAUI 8.0.70

@samhouts samhouts added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-xaml XAML, CSS, Triggers, Behaviors platform/android 🤖 platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants