diff options
author | Dominique Louis <dolouis@microsoft.com> | 2020-04-28 19:23:35 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-28 19:23:35 +0300 |
commit | d3ec80e8de373f60df70d45f717c0ed1bf875dc6 (patch) | |
tree | e801f7362b58353c8cbe049afecf875fb0dfc3ab | |
parent | 8ddbe00c8a53af7d1fa31cbb719a3fcc8471d935 (diff) |
Fix NRE when SelectionChanged is firing on a null object. (#717)
* Fix NRE when SelectionChanged is firing on a disposed object.
* Don't forget to hook the SelectionChange up again, after removing everything.
* During control re-use, SelectedValue might be null. So check for that.
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/PredefinedValuesEditor.cs | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/PredefinedValuesEditor.cs b/Xamarin.PropertyEditing.Mac/Controls/PredefinedValuesEditor.cs index 2c80314..f402bf7 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/PredefinedValuesEditor.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/PredefinedValuesEditor.cs @@ -44,10 +44,11 @@ namespace Xamarin.PropertyEditing.Mac } else { RequireComboBox (); - this.comboBox.RemoveAll (); + UnhookSelectionChangeAndClearItems (); foreach (var item in ViewModel.PossibleValues) { this.comboBox.Add (new NSString (item)); } + this.comboBox.SelectionChanged += ComboBox_SelectionChanged; } base.OnViewModelChanged (oldModel); @@ -134,9 +135,7 @@ namespace Xamarin.PropertyEditing.Mac { if (this.comboBox == null) return; - - this.comboBox.SelectionChanged -= ComboBox_SelectionChanged; - this.comboBox.RemoveAll (); + UnhookSelectionChangeAndClearItems (); this.comboBox.RemoveFromSuperview (); this.comboBox.Dispose (); this.comboBox = null; @@ -185,9 +184,19 @@ namespace Xamarin.PropertyEditing.Mac private void ComboBox_SelectionChanged (object sender, EventArgs e) { - if (ViewModel != null && this.comboBox != null) { + if (ViewModel != null + && this.comboBox != null + && this.comboBox.SelectedValue != null) { ViewModel.ValueName = this.comboBox.SelectedValue.ToString (); } } + + private void UnhookSelectionChangeAndClearItems () + { + if (this.comboBox != null) { + this.comboBox.SelectionChanged -= ComboBox_SelectionChanged; + this.comboBox.RemoveAll (); + } + } } } |