首页 > 解决方案 > 在 UWP 中显示一个简单的客户列表

问题描述

所以我目前从这里复制/粘贴一些代码:https ://docs.microsoft.com/en-us/windows/uwp/get-started/display-customers-in-list-learning-track

代码如下所示:

xaml:

<ListView ItemsSource="{x:Bind Customers}"
HorizontalAlignment="Center"
VerticalAlignment="Center">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:Customer">
            <TextBlock Text="{x:Bind Name}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

关于x:Bind Customers:它似乎不会像错误的那样自动完成。

关于x:DataType="local:Customer":我收到错误消息The name "Customer" does not exist in the namespace "using:helloUWP"

cs:

namespace helloUWP
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 

    public class Customer
    {
        public string Name { get; set; }
    }

    public sealed partial class MainPage : Page
    {
    public ObservableCollection<Customer> Customers { get; }
= new ObservableCollection<Customer>();

    public MainPage()
    {
        this.InitializeComponent();

        this.Customers.Add(new Customer() { Name = "Name1" });
    }
}

}

我无法建造它。我错过了什么或做错了什么?

标签: c#xamluwpuwp-xaml

解决方案


要在 XAML 中使用自定义类,首先必须在根元素中声明适当的命名空间,例如 for Page

<Page ... xmlns:models="TheNamespaceWhereCustomerIs">

然后使用:

x:DataType="models:Customer"

推荐阅读