删除绑定到ListView

时间:2016-09-28 07:47:41

标签: c# xamarin.forms realm realm-net

我有一个带有ListView的Xamarin XAML页面。列表数据源链接到一个可观察的集合,该集合填充了领域对象。

当我尝试从ListView中删除一个对象时(首先从数据源然后从领域中删除)我已经崩溃了。

崩溃发生在稍后仅在iOS上的绑定时间,内部异常消息是:

  

此对象已分离。它是从领域中删除的吗?

因此,似乎在删除后仍然可以从ListView访问该对象。

堆栈跟踪:

  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00050] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:313 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/reflection/methodbase.cs:229 
  at Xamarin.Forms.BindingExpression+BindingExpressionPart.TryGetValue (System.Object source, System.Object& value) [0x00043] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:498 
  at Xamarin.Forms.BindingExpression.Unapply () [0x00030] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:90 
  at Xamarin.Forms.Binding.Unapply () [0x0000e] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Binding.cs:142 
  at Xamarin.Forms.BindableObject.ApplyBindings (System.Object oldContext, System.Boolean skipBindingContext) [0x0002e] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:407 
  at Xamarin.Forms.BindableObject.ApplyBindings (System.Object oldContext) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:123 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x0005a] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:117 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:472 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:322 
  at Xamarin.Forms.View.OnBindingContextChanged () [0x00042] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\View.cs:99 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x00061] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:118 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:472 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:322 
  at Xamarin.Forms.View.OnBindingContextChanged () [0x00042] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\View.cs:99 
  at Xamarin.Forms.Grid.OnBindingContextChanged () [0x00006] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Grid.cs:139 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x00061] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:118 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:472 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:322 
  at Xamarin.Forms.View.OnBindingContextChanged () [0x00042] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\View.cs:99 
  at Xamarin.Forms.Grid.OnBindingContextChanged () [0x00006] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Grid.cs:139 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x00061] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:118 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:472 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:322 
  at Xamarin.Forms.Cell.OnBindingContextChanged () [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Cells\Cell.cs:117 
  at Xamarin.Forms.BindableObject.BindingContextPropertyBindingPropertyChanged (Xamarin.Forms.BindableObject bindable, System.Object oldvalue, System.Object newvalue) [0x0001b] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:429 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x00108] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:584 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0014b] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:378 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0005f] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:531 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:83 
  at Xamarin.Forms.BindableObject.set_BindingContext (System.Object value) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:24 
  at Xamarin.Forms.TemplatedItemsList`2+<UnhookItem>d__153[TView,TItem].MoveNext () [0x000a7] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:1210 

InnerException StackTrace:

  at Realms.NativeException.ThrowIfNecessary (System.Func`2[T,TResult] overrider) [0x00014] in <fb8acf22c75e4f8ba1fe5f730039e63b>:0 
  at Realms.RowHandle.get_RowIndex () [0x00009] in <fb8acf22c75e4f8ba1fe5f730039e63b>:0 
  at Realms.RealmObject.GetObjectValue[T] (System.String propertyName) [0x00000] in <fb8acf22c75e4f8ba1fe5f730039e63b>:0 
  at TestProject.StockApp.Data.ManagedObjects.ArticleEntity.get_Article () [0x00006] in <bc12041acc564a39b37d8658934e3bc3>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:305

更新

似乎只有在ListView中使用的Realm对象具有也显示给ListView单元格的关系时才会发生错误。

例如,如果我有一个Dogs列表但是在单元格中我还显示了OwnerFirstName,那么当我删除所选择的狗项时会发生崩溃。

public class Dog : RealmObject 
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person Owner { get; set; }
}

public class Person: RealmObject 
{
    public string FirstName{ get; set; }
}

2 个答案:

答案 0 :(得分:1)

  

列表数据源链接到一个可观察的集合,该集合填充了领域对象

该可观察集合如何更新?

堆栈跟踪在表单中很深 - 看起来好像是他们的代码如何工作的怪癖 - 请你把源代码发送到support@realm.io我会看看它。

答案 1 :(得分:0)

v0.81.0开始,RealmObject隐式为ObservableObject,因此会触发INotifyPropertyChangedINotifyCollectionChangedIListIQueryable 1}}属性。

欲了解更多信息: