Avalonia Unable to resolve property or method of name 'xxx' on type 'XamlX.TypeSystem.XamlPseudoType'解决办法

近期我在使用Avalonia编写桌面程序时,用到了ItemRepeater组件,写出来大概是这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<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,定义大概如下

1
2
3
4
5
6
7
8
9
public class ExploreViewModel : ViewModelBase
{
public A[] B { get; set; } = new A[] { ... };
}

public class A{
int Id,
string Name
}

造成该问题的原因是我需要使用的属性B是一个原始的数组,这里应该使用ObservableCollection因此解决方案是做出如下修改

1
2
3
4
public class ExploreViewModel : ViewModelBase
{
public ObservableCollection<Assistant> Assistants { get; set; } = new (new List<Assistant>{...});
}

至此能够成功编译运行

Avalonia Unable to resolve property or method of name 'xxx' on type 'XamlX.TypeSystem.XamlPseudoType'解决办法

http://blog.sworld.club/2024/09/79759d31/

作者

Sworld

发布于

2024-09-26

更新于

2024-09-26

许可协议

评论