2016-01-27 32 views
0

``我爲什麼我收到此錯誤心亂如麻:項目添加到的ObservableCollection是全成,但仍然拋出異常

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.ObservableCollection 1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection 1.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> 

視圖模型屬性:

 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; } 
    } 

UPDATE

這行代碼改變了我的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); 

因此創建一個對象的副本並將其添加到我的觀察失敗。但添加原始對象成功。

回答

1

這是關於Windows通用平臺(即Windows 10)的Xamarin.Forms錯誤。

而不是調用我的UI數據綁定到的ObservableCollection上的添加操作,我只是爲每個添加操作創建一個新的ObservableCollection並傳入構造函數中的集合。

解決方法:

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

您的屬性是隻讀的。我會改變的屬性有一個私定

 public EmployeeId EmployeeId { get; private set; } 

     public bool IsValid { get; private set;} 
+0

感謝肯。我試過了。但相同的結果。 –

0

你的第二個BadgeScan構造函數不初始化EmployeeId財產。在這一行:

EmployeeIds.Add(badge.EmployeeId); 

...這似乎你可能會添加null對象添加到集合。這本身不應該是一個問題,但是您在數據綁定中使用了EmployeeId.Value。我的猜測是NRE與此有關。


更新:@ScottNimrod說,這是一個錯誤Xamarin,在這種情況下,NRE可能無法在本畢竟相連。即使如此:EmployeeId的故意設置是不是?

相關問題