添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

Might as well file the bug officially.

Using Avalonia 11.0.999-cibuild0030749-beta .

With this in csproj

<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>

Let's create a compiled binding

<Button Content="Action!" VerticalAlignment="Bottom" Width="120"
    Command="{Binding ViewModel.Action, RelativeSource={RelativeSource AncestorType={x:Type views:MainView}}}" />

It gives a compile error

MainView.axaml(25, 17): [AVLN:0004] AncestorType must be set for RelativeSourceMode.FindAncestor when searching the visual tree. Line 25, position 17.

Here's a repo project
Avalonia11-RelativeSource.zip

The work-around is to replace the binding with this, and it works

"{Binding $parent[views:MainView].ViewModel.Action}"
          

Alright, we have tests for that.
But expected syntax was Command="{Binding ViewModel.Action, RelativeSource={RelativeSource AncestorType=views:MainView}}" />
I.e., without "x:Type". Will take a look, why it doesn't understand "x:Type".

@mysteryx93 not really, as with latest master there is Inter font issue. "preivew5" works.
One note though:
You have "Roots.xml" with Fluent and Avalonia11 assemblies in it. Fluent can be removed from there. And Avalonia11 can be removed, if you avoid reflection in the view locator (replace reflection with static dictionary of viewmodel->view registrations, for example). It can improve trimming experience.

I'm observing an interesting difference between

{Binding $parent[views:MainView].ViewModel.ShowAbout}
{Binding ViewModel.ShowAbout, RelativeSource={RelativeSource AncestorType=views:MainView}}

With $parent, DataContext is null when it is executed; but with the other syntax, it works.

Why such behavioral difference? I verified that ViewModel property gets called. It means it's being called before DataContext is set.

There's a warning at the end of this docs page, it's probably related: https://docs.avaloniaui.net/docs/data-binding/binding-to-controls#binding-to-an-ancestor:

Avalonia also supports WPF/UWP's RelativeSource syntax which does something similar, but is not the same. RelativeSource works on the visual tree whereas the syntax given here works on the logical tree.