【问题标题】:Display a simple customer list in UWP在 UWP 中显示一个简单的客户列表
【发布时间】:2018-10-02 20:05:34
【问题描述】:

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

}

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

【问题讨论】:

  • 如果没有看到完整的源代码来判断“客户”实际上在哪个名称空间中并给您一个正确的答案,您也可以完全删除 x:DataType 声明,并在 DateTemplate 中替换 @ 987654328@ 与{Binding ...}
  • x:Bind 实际上是在 uwp 中更有效的绑定方式绑定消耗更多资源@JohnnyWestlake
  • 尝试使用不同的架构(x86 或 x64)清理和重建解决方案,因为如果 Customer 和 MainPage 在命名空间中,那么您的代码应该可以工作。
  • @JohnnyWestlake 我现在用完整的代码编辑了我的帖子

标签: c# xaml uwp uwp-xaml


【解决方案1】:

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

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

然后使用:

x:DataType="models:Customer"

【讨论】:

  • 因为看起来客户已经与 MainPage 在同一个命名空间中,但当然这是我的猜测
  • 在这种情况下,我不确定是否会出错:-D
  • 代码看起来不错,对我来说它看起来是一个 vs 错误,这个奇怪的错误有时可以通过清理解决方案来解决:P
猜你喜欢
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多