添加到 ObservableCollection 的项目成功但仍然抛出异常

Item added to ObservableCollection is successfull but still throws exception

``我对为什么会收到此错误感到非常困惑:

System.NullReferenceException occurred HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=Xamarin.Forms.Platform.UAP StackTrace: at Xamarin.Forms.Platform.UWP.WindowsBasePlatformServices.get_IsInvokeRequired() at Xamarin.Forms.ListProxy.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at Xamarin.Forms.ListProxy.WeakNotifyProxy.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item) at ViewModels.ScanBadgesViewModel.Add(BadgeScan result) InnerException:

错误来自以下行:

EmployeeIds.Add(badge.EmployeeId)

注意: 在 Xamarin.Forms Windows 10 通用应用程序(预览版)上观察到此错误。

如果我注释掉 XAML 中的 ListView 元素,那么我将不再收到 null 异常。

如果我只注释掉ListView的ItemTemplate,那么我仍然观察到null异常。

XAML:

    <Grid Grid.Row="4" Grid.RowSpacing="3" Grid.ColumnSpacing="3" BackgroundColor="Silver">
        <ListView ItemsSource="{Binding EmployeeIds}" SelectedItem="{Binding SelectedEmployeeId}"
                  BackgroundColor="Black">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <ViewCell.View>
                    <Label Text="{Binding Value}" TextColor="Yellow" XAlign="Start" />
                </ViewCell.View>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
      </Grid>

ViewModel 属性:

        ObservableCollection<EmployeeId> _employeeIds = new ObservableCollection<EmployeeId>();
        public ObservableCollection<EmployeeId> EmployeeIds
        {
            get { return _employeeIds; }
            set
            {
                if (_employeeIds != value)
                {
                    _employeeIds = value;
                    OnNotifyPropertyChanged();
                }
            }
        }

实体:

    public class EmployeeId
    {
        public EmployeeId(string employeeId) { Value = employeeId; }

        public string Value { get; }
    }

    public class BadgeScan
    {
        public BadgeScan(string employeeId) { EmployeeId = new EmployeeId(employeeId); }

        public BadgeScan(BadgeScan source, Predicate<string> validate) : this(source.EmployeeId.Value)
        {
            IsValid = validate.Invoke(source.EmployeeId.Value);
        }

        public EmployeeId EmployeeId { get; }

        public bool IsValid { get; }
    }

更新

这行代码改变了我的 ObservableCollection.Add 操作的行为:

var administeredScan = new BadgeScan(result, validate);

该行只是创建对象的副本。

var validate = DependencyManager.Resolve(typeof(Predicate<string>)) as Predicate<string>;

var administeredScan = new BadgeScan(result, validate);
var canAdd = CanAdd(administeredScan) && 
             ScanMode == Entities.ScanMode.Add;

if (canAdd) Add(administeredScan);
break;

即使将项目添加到集合中,这仍然会引发异常: Add(administeredScan);

然而,这成功了:

var result = obj as BadgeScan;
Add(result);

因此创建一个对象的副本以添加到我的可观察对象失败了。但是添加原始对象成功了。

您的属性是只读的。我会更改属性以拥有私有集

        public EmployeeId EmployeeId { get; private set; }

        public bool IsValid { get; private set;}

您的第二个 BadgeScan 构造函数没有初始化 EmployeeId 属性。在这一行中:

EmployeeIds.Add(badge.EmployeeId);

...您可能正在向集合中添加一个 null 对象。这本身应该不是问题,但您在数据绑定中使用 EmployeeId.Value 。我的猜测是 NRE 与此有关。


Update:@ScottNimrod 说这是一个 Xamarin 错误,在这种情况下,NRE 可能最终与此无关。即便如此:EmployeeId的non-setting是故意的吗?

这是关于 Windows 通用平台(即 Windows 10)的 Xamarin.Forms 错误。

我的 UI 是 data-bound 的 ObservableCollection 上没有调用 Add 操作,我只是为每个 Add 操作创建一个新的 ObservableCollection 并在构造函数中传入一个集合。

解决方法:

_employeeIdsHack.Add(administeredScan.EmployeeId);
EmployeeIds = new ObservableCollection<EmployeeId>(_employeeIdsHack);