近期我在使用 Avalonia 编写桌面程序时,用到了 ItemRepeater 组件,写出来大概是这样
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
xmlns:vm="clr-namespace:CooingOwl.ViewModels"
xmlns:suki="clr-namespace:SukiUI.Controls;assembly=SukiUI"
x:DataType="vm:ExploreViewModel"
x:Class="CooingOwl.Views.ExploreView">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<ItemsRepeater ItemsSource="{Binding B}" Margin="16">
<ItemsRepeater.Layout>
<StackLayout Spacing="20"
Orientation="Vertical" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<suki:GlassCard>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<TextBlock Margin="4 0" FontWeight="Bold"
Text="{Binding Id}"/>
</StackPanel>
</suki:GlassCard>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</ScrollViewer>
</UserControl>
其中<TextBlock Text="{Binding Name}"/>
和Text="{Binding Id}"/>
产生了报错AVLN2000 Unable to resolve property or method of name 'Id' on type 'XamlX.TypeSystem.XamlPseudoType'.
,查看类型ExploreViewModel
,定义大概如下
public class ExploreViewModel : ViewModelBase
{
public A[] B { get; set; } = new A[] { ... };
}
public class A{
int Id,
string Name
}
造成该问题的原因是我需要使用的属性 B 是一个原始的数组,这里应该使用ObservableCollection
因此解决方案是做出如下修改
public class ExploreViewModel : ViewModelBase
{
public ObservableCollection<Assistant> Assistants { get; set; } = new (new List<Assistant>{...});
}
至此能够成功编译运行