单击完成按钮时自定义选择器渲染器iOS崩溃

时间:2019-06-26 08:16:33

标签: ios xamarin xamarin.forms picker

我想在Xamarin Forms上创建一个带有两个轮子的选择器。我在Android上变得很容易,但是在iOS上却遇到了麻烦。

当我点击“完成”按钮或失去焦点时,选择器崩溃。

要创建我的选取器,我刚刚制作了一个继承自PickerRenderer的渲染器,然后创建了一个PickerModel,其中有两个轮毂的2个选项卡。 之后,我将模型设置为OnElementChanged中的选择器。

但是当我想关闭选择器时,应用程序崩溃了,我错过了什么?

PS:我已经尝试过Control.ResigneFirstResponder或  Control.Unfocus(),但始终会引发InvalidCastException

//There is my PickerModel
public partial class PickerModel : UIPickerViewModel
        {
            private int[] hours = Enumerable.Range(0, 1001).ToArray();
            private int[] minutes = Enumerable.Range(0, 60).ToArray();

            public override nint GetComponentCount(UIPickerView pickerView)
            {
                return 2;
            }

            public override nint GetRowsInComponent(UIPickerView pickerView, nint component)
            {
                switch (component)
                {
                    case 0: return hours.Length;
                    case 1: return minutes.Length;
                    default: break;
                }
                return 0;
            }

            public override string GetTitle(UIPickerView pickerView, nint row, nint component)
            {
                // Returns
                switch (component)
                {
                    case 0: return hours[row].ToString();
                    case 1: return minutes[row].ToString();
                    default: break;
                }
                return null;
            }

            public override nfloat GetComponentWidth(UIPickerView pickerView, nint component)
            {
                switch (component)
                {
                    case 0: return 100.0f;
                    case 1: return 100.0f;
                    case 2: return 100.0f;
                    default: break;
                }

                return 0;
            }

            public override nfloat GetRowHeight(UIPickerView pickerView, nint component)
            {
                return 40f;
            }

        }



//and this is OnElementChanged
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);

            picker = Element;
            UITextField textField = Control;
            UIPickerView pickerView = textField.InputView as UIPickerView;


            pickerView.BackgroundColor = UIColor.White;
            pickerView.Model = new PickerModel();

            hours = ((CustomTimeSpanPicker)Element).Hours;
            minutes = ((CustomTimeSpanPicker)Element).Minutes;

        }

0 个答案:

没有答案