AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.19k stars 2.18k forks source link

StyleInclude/ResourceInclude should fallback to runtime reflection, when referenced assembly is not a compile time dependency #16570

Closed AndreyAbdulkayumov closed 1 month ago

AndreyAbdulkayumov commented 1 month ago

Describe the bug

Using Avalonia UI version 11.1.11, it`s not possible to build a solution if the project uses styles declared in another project. If you use version 11.0.12, then there is no problem.

This bug only appears if you use styles from another project. If you use the styles declared in the current project, the bug does not appear.

To Reproduce

  1. Update all avalonia projects to version 11.1.1.
  2. Create a style in the project being launched. Or use that already exist. Styles files should be like as "Avalonia XAML" .
  3. In another avalonia project try use style via StyleInclude in XAML code. For example: `

    `

  4. Build solution and get error AVLN2000.

Expected behavior

The solution is being successfully assembled.

Avalonia version

11.1.1

OS

Windows

Additional context

No response

maxkatz6 commented 1 month ago

it`s not possible to build a solution if the project uses styles declared in another project

It is possible, we have plenty of these. Including DataGrid styles that are the most used.

Create a style in the project being launched

Do I understand correctly, that you create style in the executable project, and try to reference this style in the dependency? I.e. reverse depending on it? Something that is not even possible in .NET C#.

It might have been possible with reflection based StyleInclude, but we don't use that since 11.0. It shouldn't have broken suddenly in 11.1.

Could you send a sample project to make it clearer?

AndreyAbdulkayumov commented 1 month ago

I created a simple example. You can download this zip archive.

BugXAML.zip

And using avalonia 11.1.1 I get this errors image

maxkatz6 commented 1 month ago

Yes, your ExternalProject doesn't have a dependency on BugXAML project.

It's not a bug. That's how .NET works. It was previously working only because reflection usage was permitting while unsafe.

maxkatz6 commented 1 month ago

Changed the title.

On this line we can report warning (enabled as error by default) instead of forced compile error: https://github.com/AvaloniaUI/Avalonia/blob/11.1.1/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/GroupTransformers/XamlIncludeGroupTransformer.cs#L101 By doing so users will be able to redefine error severity and continue using unsafe StyleInclude/ResourceInclude.

AndreyAbdulkayumov commented 1 month ago

Yes, you're right. I moved the style to another project created as a class library. Then I added a dependency on this project to the project where this style is used. And build was successful! Issue closed. Thanks.