MVVMCross Binding Crashes Android Application

后端 未结 2 1141
慢半拍i
慢半拍i 2021-01-22 08:56

I have an Android App based on Xamarin and MvvmCross. In that application there is a view with an ExpandableListView that I created on my own. Now this list displays several ite

相关标签:
2条回答
  • 2021-01-22 09:05

    It's a bit hard to follow the code snippet provided, but I would guess that the problem you are seeing is due to the fact that you are recycling the cell and that each time you recycle it you are:

    • creating a new cell child view,
    • creating a new binding,
    • clearing the old child view
    • but not removing the old binding.

    One way to solve this might be to use the WithClearBindingKey fluent method to allow you to clear these bindings.

    For example, if a binding is created as:

     var set = this.CreateBindingSet<Cell, CellViewModel>();
     set.Bind(text).To(vm => vm.TextValue).WithClearBindingKey("MyDynamicBindings");
     set.Apply();
    

    then just the bindings created with this tag can be cleared using:

     this.ClearBindings("MyDynamicBindings");
    
    0 讨论(0)
  • 2021-01-22 09:23

    Thanks stuart, but onestly I only pasted some of the code I wrote as my listadapter has become quite complex yet. So actually I already was recycling the controls that I created in code. However it seems I found the solution. I watched my app crashes lately and they all seemed to be because some binding calls the setter of an android widget (EditText, Spinner, Checkbox, etc.) which resulted in a native error. So I placed trace messages into the "Dispose" and "JavaFinalize" methods and as it turns out, the error always happened after some "JavaFinalize" calls. So what I did was to add the following code to all the controls, that I implemented as a "Bindable"something. (like the BindableEditText in the snippet above)

        protected override void JavaFinalize()
        {
            if (this.BindingContext != null)
                this.BindingContext.ClearAllBindings();
            base.JavaFinalize();
        }
    

    So that totally does the job!

    WARNING: I also had to add this to the "MvxListItemView". So maybe it should be added to the mvvmcross sources as well?

    0 讨论(0)
提交回复
热议问题