简体   繁体   中英

Display a simple customer list in UWP

So Im currently copy/paste some code from here: https://docs.microsoft.com/en-us/windows/uwp/get-started/display-customers-in-list-learning-track

The code looks like this:

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>

About x:Bind Customers : it does not seem to autocomplete like it is wrong.

About x:DataType="local:Customer" : I get the error message 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" });
    }
}

}

I cannot build it. What am I missing or doing wrong?

To use a custom class in XAML, you first must declare the appropriate namespace in the root element, like for Page :

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

And then use:

x:DataType="models:Customer"

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