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



所以我目前从这里复制/粘贴一些代码:https://learn.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" });
}
}

}

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

若要在 XAML 中使用自定义类,首先必须在根元素中声明相应的命名空间,如Page

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

然后使用:

x:DataType="models:Customer"

最新更新