在UWP中显示简单的客户列表

时间:2018-10-02 11:56:23

标签: c# xaml uwp uwp-xaml

因此,我目前正在此处复制/粘贴一些代码: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" });
    }
}

}

我无法构建它。我想念什么或做错什么了?

1 个答案:

答案 0 :(得分:1)

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

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

然后使用:

x:DataType="models:Customer"