C# 在UWP中使用设计时保存为XAML的样本数据

C# 在UWP中使用设计时保存为XAML的样本数据,c#,data-binding,uwp,C#,Data Binding,Uwp,我正在阅读并尝试使用ListView的示例数据在设计时显示数据。 但是,我无法做到这一点,我看到一个错误,即它无法在下面突出显示的部分的ListView中构建DataContext d:DataContext=“{d:DesignData SampleData.xaml}” Visual Studio 2019 16.9.1 文件如下。这些文件已保存在flat中的一个文件夹中 你能帮我知道这样使用样本数据的适当方法吗 MainPage.xaml: <Page x:Class=&qu

我正在阅读并尝试使用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>
<sample:PersonCollection
    xmlns:sample="using:App1">
    <sample:Person Name="John Doe" Age="20" />
    <sample:Person Name="Jane Doe" Age="30" />
</sample:PersonCollection>

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

        public Person() {
        }
    }
}
using System.Collections.ObjectModel;

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

<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>
<sample:PersonCollection
    xmlns:sample="using:App1">
    <sample:Person Name="John Doe" Age="20" />
    <sample:Person Name="Jane Doe" Age="30" />
</sample:PersonCollection>

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

        public Person() {
        }
    }
}
using System.Collections.ObjectModel;

namespace App1{
    public class PersonCollection : ObservableCollection<Person> {
        public PersonCollection(): base(){
        }
    }
}
使用System.Collections.ObjectModel;
名称空间App1{
公共类PersonCollection:ObservableCollection{
public PersonCollection():base(){
}
}
}
在UWP中使用设计时保存为XAML的样本数据

恐怕您不能使用UWP应用程序,从本文档中派生, 一个好的选择是Blend for Visual Studio中的“从类创建示例数据”功能。您可以在数据面板顶部的一个按钮上找到该命令。不幸的是


只有针对.NET Framework的项目才支持in-Blend。UWP项目或以.NET核心为目标的项目不支持它。

谢谢你,Nico,谢谢你的回答。我认为这是可行的,因为这篇文章是在UWP部分。但即使不可能,也能得到确认是件好事。尽管如此,如果我像本文中的示例所示创建了一个对象,我仍然可以在设计时使用示例数据,现在我还可以使用它。