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

Custom ResourceDictionary fails to load in .NET 9 app #26348

Closed
dotMorten opened this issue Dec 3, 2024 · 4 comments · Fixed by #26355
Closed

Custom ResourceDictionary fails to load in .NET 9 app #26348

dotMorten opened this issue Dec 3, 2024 · 4 comments · Fixed by #26355
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-9.0.21 platform/windows 🪟 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version t/bug Something isn't working
Milestone

Comments

@dotMorten
Copy link
Contributor

dotMorten commented Dec 3, 2024

Description

When create a subclass of ResourceDictionary with merged dictionaries for a custom style library targeting .net8.0, the following code is generated:

[GeneratedCode("Microsoft.Maui.Controls.SourceGen", "1.0.0.0")]
private void InitializeComponent()
{
	ResourceDictionary resourceDictionary = new ResourceDictionary();
	NameScope nameScope = new NameScope();
	Uri source;
	resourceDictionary.SetAndLoadSource(source = new Uri("Resources/Styles/Brushes.xaml;assembly=MyStyleAssembly", UriKind.RelativeOrAbsolute), "Resources/Styles/Brushes.xaml", typeof(MyStyleAssembly).Assembly, (IXmlLineInfo)new XmlLineInfo(7, 29));
       ...

In .NET 9, the SetAndLoadSource was changed to internal here, which causes this generated code to fail when referencing the assembly from a nuget package (for some reason I don't quite understand, it doesn't happen with project reference).

Image

If you target .NET 8, the crash does not occur. This change was a breaking binary change that caused a regression. It's probably better to just mark the method non-browsable.

Steps to Reproduce

  1. Open the attached project: MauiResources.zip
  2. Make sure the class library is set to release build (doesn't repro if the class library is compiled for debug)
  3. Observe the above mentioned error.
  4. Change the app target to 8.0 and notice the app no longer crashes.

Link to public reproduction project repository

No response

Version with bug

9.0.12 SR1.2

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.100 SR10

Affected platforms

Android, Windows, macOS, iOS, Other (Tizen, Linux, etc. not supported by Microsoft directly)

Affected platform versions

No response

Did you find any workaround?

Add net9 target to class library and reference 9.0.0 of Microsoft.Maui.Controls. This only works if you have access to that library though.

Relevant log output

@simonrozsival
Copy link
Member

for some reason I don't quite understand, it doesn't happen with project reference

My best guess is that when you use project reference, the XAML code is rebuilt with XamlC in .NET MAUI 9 which generates code that doesn't need SetAndLoadSource. The NuGet package contains the pre-built XAML though, so it will still call SetAndLoadSource.

@dotMorten
Copy link
Contributor Author

Sorry ignore my comment on project reference. That was from before I realized it was only release builds causing it and forgot to update that comment. Reproducer shows it reproduces with project reference.

@Ying-6
Copy link

Ying-6 commented Dec 4, 2024

This issue has been verified using Visual Studio 17.13 Preview 1 (9.0.12 & 9.0.10). Not repro this issue on Windows Platform.

@Ying-6 Ying-6 added s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version labels Dec 4, 2024
Copy link
Contributor

Hi @dotMorten. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@jfversluis jfversluis added this to the .NET 9 SR2 milestone Dec 4, 2024
@samhouts samhouts added area-xaml XAML, CSS, Triggers, Behaviors fixed-in-9.0.21 labels Dec 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 fixed-in-9.0.21 platform/windows 🪟 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants