我按照以下方式创建UIPickerView
UIPickerView pv = new UIPickerView(new CoreGraphics.CGRect(10, 20, 100, 70));
点击按钮,我调用以下内容。
public void HandleClick(object sender, EventArgs e)
{
int index = (int)pv.SelectedRowInComponent(0);
if (index == 0)
index = 1;
else if (index == 1)
index = 2;
else if (index == 2)
index = 0;
pv.Select(index, 0, false);
}
上面代码中的最后一行不会触发Selected事件,但如果我通过与UI交互自行更改UIPickerView选项,则会调用Selected。
这是我的UIPickerViewModel类。
public class PickerModel : UIPickerViewModel
{
string[] values = new string[] {"Item 1", "Item 2","Item 3" };
public event EventHandler ItemSelected;
public override nint GetRowsInComponent(UIPickerView pickerView, nint component)
{
return values.Length;
}
public override nint GetComponentCount(UIPickerView pickerView)
{
return 1;
}
public override string GetTitle(UIPickerView pickerView, nint row, nint component)
{
return values[(int)row].ToString();
}
public override void Selected(UIPickerView pickerView, nint row, nint component)
{
ItemSelected(pickerView, EventArgs.Empty);
}
}
这是预期的行为,还是我需要做一些不同的事情来触发Selected事件?在Android中,在代码触发器中设置Spinner的选定索引。