在 Avalonia ItemsControl 中选择基于 ViewModel 类型的 DataTemplate

Selecting a DataTemplate based on ViewModel type in Avalonia ItemsControl

里面有UserControl和ItemsControl,需要根据Items集合元素类型显示不同DataTemplate的items。

以下 TemplateSelector 和 XAML 根据此答案创建的 ()

    <ItemsControl Items="{Binding Items}">
        <ItemsControl.DataTemplates>
            <views:ItemsTemplateSelector>
                <DataTemplate x:Key="{x:Type itemViewModels:Item1ViewModel}">
                    <itemsViews:Item1View/>
                </DataTemplate>
                <DataTemplate x:Key="{x:Type itemViewModels:Item2ViewModel}">
                    <itemsViews:Item2View/>
                </DataTemplate>
            </views:ItemsTemplateSelector>
        </ItemsControl.DataTemplates>
    </ItemsControl>
        public ItemsViewModel()
        {
            this.Items = new ObservableCollection<IItemViewModel>();
            this.Items.Add(new Item1ViewModel("Item1"));
            this.Items.Add(new Item2ViewModel("Item2"));
        }

        public ObservableCollection<IitemViewModel> Items { get; }
    public class ItemsTemplateSelector : IDataTemplate
    {
        public bool SupportsRecycling => false;

        [Content]
        public Dictionary<Type, IDataTemplate> Templates { get; } = new Dictionary<Type, IDataTemplate>();

        public IControl Build(object data)
        {
            var type = data.GetType();
            var template = this.Templates[type];
            var control = template.Build(data);

            return control;
        }

        public bool Match(object data)
        {
            return data is IItemViewModel;
        }
    }
    public interface IItemViewModel
    {
        string Name { get; }
    }
    public class Item1ViewModel : IItemViewModel
    {
        public Item1ViewModel (string name)
        {
            this.Name = name;
        }

        public string Name { get; }
    }
<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"
  x:Class="Desktop.Views.Items.Item1View">

  <TextBlock Text="{Binding Name}"/>

</UserControl>

运行时出现异常:

Avalonia.Markup.Xaml.XamlLoadException: 'No precompiled XAML found for Desktop.Views.Items.Item1View, make sure to specify x:Class and include your XAML file as AvaloniaResource'

如果在 ItemsControl 而不是 <itemsViews:Item1View/> 中指定 <TextBlock Text="{Binding Name}"/>,则一切正常。但我想将每个项目的标记拆分为一个单独的 XAML 文件,其中包含单独的 ViewModel。项目可以完全不同。

是否可以解决这个问题,让 ItemsControl select 一个基于 ViewModel 类型的 DataTemplate?

为了解决这个问题,我手动编辑了项目文件 *.csproj 添加了 AvaloniaResource 元素

<AvaloniaResource Include="..\Desktop\Views\Items*.xaml">
    <SubType>Designer</SubType>
</AvaloniaResource>