简体   繁体   中英

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

I am reading this article and trying to use a sample data for ListView to display the data at the design time.
However, I cannot make it, and am seeing an error saying that it cannot build the DataContext in the ListView at the highlighted part below.

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

Visual Studio 2019 16.9.1

The files are as below. These have been saved in one folder in flat.

Could you help me to know the appropriate way of such using of sample data?

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 (marked as DesignData for the build action):

<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

I'm afraid you can't use Xaml design data for UWP app, derive from this document, One good option is the Create Sample Data from Class feature in Blend for Visual Studio. You can find that command on one of the buttons at the top of the Data panel. Unfortunately,

The Data panel in Blend is supported only for projects that target .NET Framework. It's not supported for UWP projects or projects that target .NET Core.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM