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.94k stars 2.24k forks source link

AvaloniaRuntimeXamlLoader.Parse throws System.InvalidCastException #17111

Open WilhelmJP opened 1 month ago

WilhelmJP commented 1 month ago

Describe the bug

In the shared project I tried to parse a xaml string in the MainView class. The called Parse method throw an InvalidCastException: Unable to cast object of type 'XamlX.Ast.SkipXamlValueWithManipulationNode' to type 'XamlX.Ast.IXamlAstPropertyReference'. The code is:

    public MainView()
    {
        InitializeComponent();
        try
        {
            var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui' Content='Foo'/>";
            var target = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

To Reproduce

Generate the solution with dotnet new avalonia.xplat -m CommunityToolkit -o ElvisUltra.Clients install package Avalonia.Markup.Xaml.Loader Change the MainView constructor as described. Start Debug.

Expected behavior

Should Parse without exception.

Avalonia version

11.1.3

OS

Windows

Additional context

No response

WilhelmJP commented 1 month ago

The problem was that the type Avalonia.Data.Binding could not be found. Therefore, I changed my code and added the line var _ = typeof(Binding); before the Parse method is called and everything works fine. Maybe someone knows a better solution.