【问题标题】:Xamarin.Forms Picker ItemDisplayBinding not working with static listXamarin.Forms Picker ItemDisplayBinding 不使用静态列表
【发布时间】:2019-11-12 22:06:34
【问题描述】:

我正在尝试使用不同的项目来源的选择器,当我尝试使用静态列表时,我不断收到 NullPointerException。

Xaml

<Picker ItemsSource="{x:Static Member=stat:Stat.ItemModList}"
                    ItemDisplayBinding="{Binding Name}" />

选择器是 xaml 中的唯一元素。我没有更改文件后面的代码。 stat 是静态列表文件的命名空间。

当只写&lt;Picker ItemsSource="{x:Static Member=stat:Stat.ItemModList}" /&gt; 时,选择器确实工作并且选择器项目都显示为ItemMod class'ToString()

当我添加 ItemDisplayBinding="{Binding Name} 时,它不起作用。当我单击选择器时,我得到了 nullpointerexception。

其他文件:

Stat.cs - 静态列表

public static class Stat
    {
        public static List<ItemMod> ItemModList = new List<ItemMod>()
        {
            new ItemMod {Id = -1, Name = "Default"},
            new ItemMod {Id = 1, Name = "Item 1"},
            new ItemMod {Id = 2, Name = "Item 2"},
            new ItemMod {Id = 3, Name = "Item 3"},
            new ItemMod {Id = 4, Name = "Item 4"}
        };
    }

ItemMod.cs

public class ItemMod
    {
        public int Id;
        public string Name;
    }

【问题讨论】:

  • Name 必须是公共属性,而不是字段。您只能绑定到公共属性
  • @Jason 谢谢,真不敢相信我错过了。我想是的,这是一个属性,为什么它不起作用...直到我看到您的帖子才意识到我没有添加 get set 位
  • 既然已经解决了,可以发帖接受回答吗?这样其他人就不会费心打开这个问题,因为它会被标记为“已回答”。

标签: c# xaml xamarin xamarin.forms xamarin.android


【解决方案1】:

ItemDisplayBinding 不起作用,因为 BindingProperty Name 在 ItemMod 类中被声明为公共字段,而不是公共属性。
所以在 ItemMod.cs 中应该是 public string Name {get;set;}

【讨论】:

  • 别忘了采纳你的答案,这将帮助更多的人。
  • 这正是我的问题。选择器列表仍然是 EMPTY。我认为这就是问题所在。这个公认的答案对我没有帮助,它实际上有不同的目的。
猜你喜欢
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 2023-02-22
相关资源
最近更新 更多