在 UWP 的设计时使用保存为 XAML 的示例数据

Using Sample Data saved as XAML, at Design Time in UWP

我正在阅读 this article 并尝试使用 ListView 的样本数据在设计时显示数据。
但是,我做不到,并且看到一条错误消息,指出它无法在下面突出显示的部分的 ListView 中构建 DataContext。

d:DataContext="{d:DesignData SampleData.xaml}"

Visual Studio 2019 16.9.1

文件如下。这些都保存在flat的一个文件夹里。

你能帮我知道这样使用示例数据的适当方式吗?

MainPage.xaml:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    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"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" 
    d:DesignWidth="459" d:DesignHeight="262">
    <Grid>
        <ListView d:DataContext="{d:DesignData SampleData.xaml}"
                  ItemsSource="{Binding Mode=OneWay}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBox Text="-- " />
                        <TextBox Text="{Binding Name}" />
                        <TextBox Text="{Binding Age}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>

SampleData.xaml(标记为构建操作的设计数据):

<sample:PersonCollection
    xmlns:sample="using:App1">
    <sample:Person Name="John Doe" Age="20" />
    <sample:Person Name="Jane Doe" Age="30" />
</sample:PersonCollection>

Person.cs:

namespace App1 {
    public class Person  {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person() {
        }
    }
}

PersonCollection.cs:

using System.Collections.ObjectModel;

namespace App1{
    public class PersonCollection : ObservableCollection<Person> {
        public PersonCollection(): base(){
        }
    }
}

Using Sample Data saved as XAML, at Design Time in UWP

恐怕你不能将 Xaml design data 用于 UWP 应用程序,从这篇文档中得出, 一个不错的选择是 Blend for Visual Studio 中的“从 Class 创建示例数据”功能。您可以在“数据”面板顶部的按钮之一上找到该命令。很遗憾,

Blend 中的 Data panel 仅支持面向 .NET Framework 的项目。 UWP 项目或以 .NET Core 为目标的项目不支持它。