Xamarin Forms Dispose Textview

问题描述:

在我的Xamarin Forms Project(android)中,我得到了这个异常。问题是stacktrace几乎没有提供关于如何重现的信息,或者它发生在哪里。Xamarin Forms Dispose Textview

Xamarin caused by: android.runtime.JavaProxyThrowable: System.ObjectDisposedException: Cannot access a disposed object. 
Object name: 'Xamarin.Forms.Platform.Android.FormsTextView'. 
Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable self)<be2616b5140e4cbb8821649c795cfe62>:0 
Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeNonvirtualVoidMethod(string encodedMember, IJavaPeerable self, JniArgumentValue* parameters)<be2616b5140e4cbb8821649c795cfe62>:0 
Android.Widget.TextView.set_TextFormatted(ICharSequence value)<ec6bb63c41624773b5cc1eb2222982af>:0 
Android.Widget.TextView.set_Text(string value)<ec6bb63c41624773b5cc1eb2222982af>:0 
Xamarin.Forms.Platform.Android.LabelRenderer.UpdateText()<d572ae0ccf89437fad1de1370c623a06>:0 
Xamarin.Forms.Platform.Android.LabelRenderer.OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)<d572ae0ccf89437fad1de1370c623a06>:0 
<Module>.invoke_void_object_PropertyChangedEventArgs(object, PropertyChangedEventArgs)(wrapper delegate-invoke) 
Xamarin.Forms.BindableObject.OnPropertyChanged(string propertyName)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.Element.OnPropertyChanged(string propertyName)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.SetValueActual(BindableProperty property, BindableObject.BindablePropertyContext context, object value, bool currentlyApplying, BindableObject.SetValueFlags attributes, bool silent)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, object value, BindableObject.SetValueFlags attributes, BindableObject.SetValuePrivateFlags privateAttributes)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindingExpression.ApplyCore(object sourceObject, BindableObject target, BindableProperty property, bool fromTarget)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindingExpression.Apply(object sourceObject, BindableObject target, BindableProperty property)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.Binding.Apply(object newContext, BindableObject bindObj, BindableProperty targetProperty)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.ApplyBindings(object oldContext, bool skipBindingContext)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.ApplyBindings(object oldContext)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.SetInheritedBindingContext(BindableObject bindable, object value)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.Element.SetChildInheritedBindingContext(Element child, object context)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.Element.OnBindingContextChanged()<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.View.OnBindingContextChanged()<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.SetInheritedBindingContext(BindableObject bindable, object value)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.Element.SetChildInheritedBindingContext(Element child, object context)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.Element.OnBindingContextChanged()<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.Cell.OnBindingContextChanged()<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.BindingContextPropertyBindingPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.SetValueActual(BindableProperty property, BindableObject.BindablePropertyContext context, object value, bool currentlyApplying, BindableObject.SetValueFlags attributes, bool silent)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, object value, BindableObject.SetValueFlags attributes, BindableObject.SetValuePrivateFlags privateAttributes)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.SetValue(BindableProperty property, object value)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.BindableObject.set_BindingContext(object value)<7b0368eb3d0f405da776a09e3171532a>:0 
Xamarin.Forms.TemplatedItemsList<TView, TItem>.<UnhookItem>d__140.MoveNext()<7b0368eb3d0f405da776a09e3171532a>:0 
System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(object state)<67dcc9194d6a4f4f97ac27121b4304c3>:0 
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state, bool preserveSyncCtx)<67dcc9194d6a4f4f97ac27121b4304c3>:0 
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state, bool preserveSyncCtx)<67dcc9194d6a4f4f97ac27121b4304c3>:0 
System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()<67dcc9194d6a4f4f97ac27121b4304c3>:0 
System.Threading.ThreadPoolWorkQueue.Dispatch()<67dcc9194d6a4f4f97ac27121b4304c3>:0 
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()<67dcc9194d6a4f4f97ac27121b4304c3>:0 
+0

是否使用涉及一个TextView定制呈现?这是否发生在添加了某段代码之后? –

+0

是的,我有2个renderes(自定义ImageButton和自定义Entry Renderer)里面有标签。但是很难知道哪一个可能导致问题。正如我所说,我没有关于例外发生的情况。 –

+1

我明白了,但是如果你没有任何背景,我们甚至更少,因为我们没有代码可以查看,也不知道你在做什么。尝试通过在渲染器中设置断点并通过它们或者注释行并查看哪一个导致问题来消除选项。然后,如果您仍然不知道发生了什么事情,请将您的问题与相关代码一起发布,供我们查看。 –

下面是解

创建CustomLabel和CustomLabelRenderer

在Android项目应该看起来像你的渲染代码...

public class CustomLabelRenderer: LabelRenderer 
{ 
    protected override void OnAttachedToWindow() 
    { 
     try 
     { 
      base.OnAttachedToWindow(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
     } 
    } 

    protected override void Dispose(bool disposing) 
    { 
     try 
     { 
      base.Dispose(disposing); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
     } 
    } 
} 

该解决方案是非常哈克但它适用于我100%

这个问题大多发生是因为的ListView中的TextView/TextField。如果您有同样的情况,则可以将CachingStrategy更改为RecycleElement

例子:

<ListView x:Name="AccountDetailsList" 
         StyleId="AccountDetailsListID" 
         CachingStrategy="RecycleElement" 
         ItemsSource="{Binding AccountDetails}"