I am trying to create my own assembler with three components. I followed the tutorial on the Xamarin website here . I got control over the work, except when I click the "Finish" button on the collector (this is the default button). I get the following exception:
System.InvalidCastException: it is not possible to throw an object of type 'myproj.iOS.MyPickerModel' to enter 'Xamarin.Forms.Platform.iOS.PickerRenderer + PickerSource'.
Here is my code:
In general proj:
public class MyPicker: Picker {}
In iOS proj:
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))] public class MyPickerRenderer: PickerRenderer {
And the model:
public class MyPickerModel : UIPickerViewModel { private string[] array1 = new string [] { "1","2","3","4" }; private string[] array2 = new string [] { "1a","2a","3a","4a" }; private string[] array3 = new string [] { "a","b","c","d" }; public override nint GetComponentCount (UIPickerView pickerView) { return 3; } public override nint GetRowsInComponent (UIPickerView pickerView, nint component) { // Returns switch (component) { case 0: return array1.Length; case 1: return array2.Length; case 2: return array3.Length; default:break; } return 0; } public override string GetTitle (UIPickerView pickerView, nint row, nint component) { // Returns switch (component) { case 0: return array1[row]; case 1: return array2[row]; case 2: return array3[row]; 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 finally, my page:
public class MyPage : ContentPage { public MyPage() { MyPicker picker = new MyPicker { Title = "Color", VerticalOptions = LayoutOptions.CenterAndExpand }; picker.SelectedIndexChanged += (sender, args) => { }; var mainLayout = new StackLayout { Orientation = StackOrientation.Vertical, Children = { picker } };
I do not understand why it is trying to use MyPickerModel for PickerRenderer + PickerSource
Thanks!
source share