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>{...});
}

これで、正常にコンパイルして実行できるようになります。

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。