当我尝试绑定集合项的 属性 时,对象引用未设置为对象的实例

Object reference not set to an instance of an object when I try to bind a property of the item of a collection

我正在使用 Xamrin.Forms 和 MVVM。对于标签的文本,我想使用绑定的主要对象集合的第一个元素的属性值。我正在尝试这个:

<Label Text="{Binding MainObject.Colleciton[0].MyProperty}" HorizontalTextAlignment="Start" TextColor="Black" FontSize="20" />

但是我在尝试编译时收到错误消息,指出对象引用未设置为对象的实例。

奇怪的是智能感知检测到MyProperty,所​​以我不知道为什么它需要设置一个引用,因为通常当我设置绑定时,我从来没有得到检查对象的情况是否为空。

如何绑定第一项的属性?

谢谢。

The odd thing is that the intellisense detect MyProperty

属性 链可能有效,但这并不意味着所有部分都具有有效(即非空)数据。具体来说,索引集合很可能不受 Xamarin 中的 null 检查保护。

所以,请确保您实际初始化了您的属性!