NobsterTheLobster / Xamarin.Forms.GridView

GridView for xamarin forms
MIT License
60 stars 10 forks source link

[Android]Binding not remove when itemsource reset cause crash #37

Open pardont opened 5 years ago

pardont commented 5 years ago

It seems the viewcell's BindingContext not remove when gridview.ItemsSource reset.

I have one page contains one gridview show plu info, some category button to switch gridview's ItemsSource, and a timer to sync plu info from db. But when I switch category serveral times(5-10 times), the app crashed. exception is: `Unhandled Exception:

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'Xamarin.Forms.Platform.Android.FormsTextView'.`

Attached is the page code, please help, Thanks.

PageCode.zip

NobsterTheLobster commented 5 years ago

Yeah its quite possible you have uncovered something. Your particular scenario is not something I would have tested significantly.

These suggestions will probably not make a difference but no harm in tryting since it shouldn't take too long to implement..

  1. Rather than changing the itemsource could you try just replacing the items in the itemsource. You could take a look at the observable collection class in my repo.
  2. Could you try binding to the itemsource as opposed to setting it from the code behind.

So basically in your TestBindingPage class declare a new

public partial class TestBindingPage : ContentPage, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    public _ObservableCollection<TimeModel> DataContext { get; set; } new _ObservableCollection<TimeModel>();

/// <summary>
        /// switch gridview's ItemsSource
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Clicked(object sender, EventArgs e)
        {
            lock (lockHelper)
            {
                _sourceIndex = _sourceIndex == 0 ? 1 : 0;

                DataContext.ReplaceRange(source[_sourceIndex]);
            }
        }