question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Relative Bindings Fail with CompileBindings Enabled (ReactiveUI/MVVM)

See original GitHub issue

Describe the bug I’m experiencing an issue with Avalonia 11.0.0-rc1.1 where relative bindings fail to compile when CompileBindings is enabled. I’ve tried three different syntaxes for binding to the parent window’s ViewModel, all of which yield the same error:

  • {Binding ViewModel.TestMethodCommand, RelativeSource={RelativeSource AncestorType={x:Type views:MainWindow}}}
  • {Binding #Window.ViewModel.TestMethodCommand}
  • {Binding $parent[views:MainWindow].ViewModel.TestMethodCommand}

To Reproduce Steps to reproduce the behavior:

  1. Create a new project with the Avalonia MVVM template
  2. Make sure that the MainWindow class derives from ReactiveWindow<MainWindowViewModel>, and not Window
  3. Set CompiledBindings to True on the window
  4. Implement a test command in MainWindowViewModel
  5. Place a button on the window, bind it to the test command via any of the three methods mentioned above
  6. Try to compile the application
  7. The following exception is thrown while compiling the bindings:
  MainWindow.axaml(18, 17): [AVLN:0004] Internal compiler error while emitting node Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.XamlIlBindingPathHelper+XamlIlBindingPathNode:
System.NullReferenceException: Object reference not set to an instance of an object.
   at Mono.Cecil.ImportGenericContext.TypeParameter(String type, Int32 position)
   at Mono.Cecil.DefaultMetadataImporter.ImportTypeSpecification(TypeReference type, ImportGenericContext context)
   ...

Expected behavior The program should compile successfully.

Desktop:

  • OS: Windows 11 22H2
  • Version 11.0.0-rc1.1 and 11.0.0-preview8

Additional context The error only occurs when CompileBindings is enabled. If I disable it, the code compiles and the binding works as expected. The error in question is an internal compiler error with a NullReferenceException.

The issue might be related to #10489 and #10486.

Use case The obvious answer would be to simply not use relative bindings and bind to the ViewModel directly (assuming that x:DataType is set on the control/window) which would work as expected regardless of the state of CompiledBindings. However in my specific use case, I was trying to bind to a command in a custom control’s ViewModel from within the ItemTemplate of a ListBox (a simple “delete this row” button), where I had to use relative bindings to reach my ViewModel.

This is not a critical bug in my case since I can just disable CompiledBindings for the time being, but I do enjoy the benefits of the function such as additional IntelliSense and syntax highlighting in the axaml editor when it’s enabled, so I would consider it an annoying bug.

Issue Analytics

  • State:open
  • Created 4 months ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
IanRawleycommented, Jun 2, 2023

I was having the same issue, and found that you can instead use ReflectionBinding for those specific cases. e.g. Instead of Command=“{Binding ElementName=X, Path=DataContext.DoYCommand}” do Command=“{ReflectionBinding ElementName=X, Path=DataContext.DoYCommand}”

This turns off compiled bindings for this binding only.

1reaction
timuniecommented, Jun 2, 2023

While I think in ReactiveWindow it should work without, one can also cast in the C# like cast style inside a binding. e.g.

{Binding $parent[Window].((vm:MyViewModel).DataContext).MyCommand }

Read more comments on GitHub >

github_iconTop Results From Across the Web

CompiledBinding fails with RelativeSource · Issue #10486
With this in csproj true Let's create a compiled binding. ... Relative Bindings Fail with CompileBindings Enabled (ReactiveUI/MVVM) #11616.
Read more >
Compiled Bindings
Compiled bindings are not enabled by default. To enable compiled bindings, you will need to define the DataType of the object you want...
Read more >
Xamarin.Forms Relative Bindings
This article explains how to create relative bindings by using the RelativeSource markup extension to set the binding source relative to the ...
Read more >
Compiled bindings - .NET MAUI
Compiled bindings require the use of XAML compilation, which is enabled by default in .NET MAUI. If you've disabled XAML compilation, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found