From f2eac7fa9898b02dc02d529d54776c884e84cd5a Mon Sep 17 00:00:00 2001 From: Dominique Louis Date: Tue, 27 Feb 2018 16:53:45 +0000 Subject: Fixes to Support Nullables On Mac --- .../Controls/NumericEditorControl.cs | 27 +++++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs') diff --git a/Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs index 5f326b5..42f1257 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs @@ -9,16 +9,20 @@ namespace Xamarin.PropertyEditing.Mac { internal class NumericEditorControl : PropertyEditorControl - where T : struct { public NumericEditorControl () { base.TranslatesAutoresizingMaskIntoConstraints = false; - NumericEditor = new NumericSpinEditor (); + NumericEditor = new NumericSpinEditor (); NumericEditor.ValueChanged += OnValueChanged; - TypeCode code = Type.GetTypeCode (typeof (T)); + var t = typeof (T); + if (t.Name == PropertyViewModel.NullableName) { + underlyingType = Nullable.GetUnderlyingType (t); + t = underlyingType; + } + TypeCode code = Type.GetTypeCode (t); switch (code) { case TypeCode.Double: case TypeCode.Single: @@ -41,7 +45,7 @@ namespace Xamarin.PropertyEditing.Mac }); } - protected NumericSpinEditor NumericEditor { get; set; } + protected NumericSpinEditor NumericEditor { get; set; } protected NSNumberFormatter Formatter { get { @@ -63,6 +67,14 @@ namespace Xamarin.PropertyEditing.Mac } } + internal new NumericPropertyViewModel ViewModel + { + get { return (NumericPropertyViewModel)base.ViewModel; } + set { base.ViewModel = value; } + } + + private Type underlyingType; + protected override void UpdateErrorsDisplayed (IEnumerable errors) { if (ViewModel.HasErrors) { @@ -85,12 +97,15 @@ namespace Xamarin.PropertyEditing.Mac protected virtual void OnValueChanged (object sender, EventArgs e) { - ((PropertyViewModel)ViewModel).Value = (T)Convert.ChangeType (NumericEditor.Value, typeof(T)); + var t = typeof (T); + if (underlyingType != null) + t = underlyingType; + ViewModel.Value = (T)Convert.ChangeType (NumericEditor.Value, t); } protected override void UpdateValue() { - NumericEditor.Value = (double)Convert.ChangeType (((PropertyViewModel)ViewModel).Value, typeof(double)); + NumericEditor.Value = (double)Convert.ChangeType ((ViewModel).Value, typeof(double)); } protected override void UpdateAccessibilityValues () -- cgit v1.2.3