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.42k stars 2.2k forks source link

[Regression] Using DynamicResource in a ResourceDictionary throws XamlException #16731

Open Bip901 opened 1 month ago

Bip901 commented 1 month ago

Describe the bug

In Avalonia 11.0.12, you could include DynamicResource elements in a ResourceDictionary to create aliases:

<ResourceDictionary xmlns="https://github.com/avaloniaui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Design.PreviewWith>
        <Border Padding="20">

        </Border>
    </Design.PreviewWith>

    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Light">
            <DynamicResource x:Key="CardBackground" ResourceKey="SystemRegionBrush"/>
        </ResourceDictionary>
        <ResourceDictionary x:Key="Dark">
            <SolidColorBrush x:Key="CardBackground" Color="#2c2c2c"/>
    </ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>

Since version 11.1.1, however, the Visual Studio previewer throws the following exception: designer screenshot

21:11:01.917 [Information]  Started AvaloniaDesigner.CreateCompletionMetadataAsync() for "...REDACTED.dll"
21:11:01.918 [Information]  Starting previewer process for '"...REDACTED.dll"'
21:11:01.921 [Information] 22576 Started previewer process for '"...REDACTED.dll"'. Waiting for connection to be initialized.
21:11:01.991 [Information] 22576 Connection initialized
21:11:02.830 [Error] 22576 UpdateXamlResult error
System.Xaml.XamlException: Unable to cast object of type 'Avalonia.Markup.Xaml.MarkupExtensions.DynamicResourceExtension' to type 'Avalonia.Media.IBrush'.
21:11:19.807 [Information] 22576 Stopping previewer process
21:11:19.807 [Information]  Process exited

To Reproduce

Use the AXAML above.

Expected behavior

No response

Avalonia version

11.1.1, 11.1.2, 11.1.3

OS

Windows

Additional context

Visual Studio 2022 17.11

maxkatz6 commented 1 month ago

IIRC, DynamicResource used in resources like this never worked properly.

Bip901 commented 1 month ago

Do you happen to know the recommended way to achieve key aliasing?