Object disposing in Xamarin.Forms

前端 未结 3 1251
庸人自扰
庸人自扰 2021-01-17 12:56

I\'m looking for the right way to dispose objects in a Xamarin Forms application. Currently i\'m using XAML and MVVM coding style. Then from my view model i get a reference

3条回答
  •  天命终不由人
    2021-01-17 13:31

    We were getting disposed of object exceptions in Forms when Bindings to ListViews or Labels changed values as pages/fragments were being disposed of. I'm assuming you could dispose of objects in your ViewModel the same place we were removing bindings.

    protected override void OnParentSet()
    {
        base.OnParentSet();
    
        if (Parent == null)
        {
            //Clear a bunch of bindings or dispose of ViewModel objects 
            BindingContext =
                _listView.ItemsSource = null;
        }
    }
    

提交回复
热议问题