Sworld

Sworld

The only way to do great work is to love what you do.

Avalonia 無法解析類型 'XamlX.TypeSystem.XamlPseudoType' 上名稱為 'xxx' 的屬性或方法解決辦法

近期我在使用 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>{...});
}

至此能夠成功編譯運行

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。