Sto provando a creare un selettore personalizzato con 3 componenti. Ho seguito il tutorial sul sito Xamarin here. Ho ottenuto il controllo per funzionare, tranne quando faccio clic sul pulsante Fatto sul selettore (Questo è il pulsante predefinito fatto). Ricevo la seguente eccezione:Cercando di creare un selettore personalizzato in Xamarin Forms
System.InvalidCastException: impossibile eseguire il cast dell'oggetto di tipo "myproj.iOS.MyPickerModel" per digitare "Xamarin.Forms.Platform.iOS.PickerRenderer + PickerSource".
Ecco il mio codice:
Nella proj comune:
public class MyPicker: Picker {}
Negli iOS proj:
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
public class MyPickerRenderer: PickerRenderer
{
// Override the OnElementChanged method so we can tweak this renderer post-initial setup
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var picker = (UIPickerView)this.Control.InputView;
picker.BackgroundColor = UIColor.White;
picker.Model = new MyPickerModel();
}
e il modello:
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;
}
}
E infine la mia pagina:
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
}
};
// Accomodate iPhone status bar.
this.Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
this.BackgroundImage = "background.png";
// Build the page.
this.Content = mainLayout;
}
}
non capisco il motivo per cui il suo tentativo di lanciare MyPickerModel a PickerRenderer + PickerSource
Grazie!