今天早上我遇到了同样的问题,我还想触发一个更改事件,以便在编辑数据时在对话框上添加一个“取消”按钮。这两个任务都要求您继承 RadioElement 并覆盖 Selected 方法 - 请注意额外的步骤,以确保如果用户单击已选择的项目,对话框不会关闭 - 如果您单击任何内容,即使它已经被选中,它也会触发,所以我想防止这种情况发生-我的看起来像这样。
public class MyRadioElement : RadioElement {
// Pass the caption through to the base constructor.
public MyRadioElement (string pCaption) : base(pCaption) {
}
// Fire an event when the selection changes.
// I use this to flip a "dirty flag" further up stream.
public override void Selected (
DialogViewController pDialogViewController,
UITableView pTableView, NSIndexPath pIndexPath) {
// Checking to see if the current cell is already "checked"
// prevent the event from firing if the item is already selected.
if (GetActiveCell().Accessory.ToString().Equals(
"Checkmark",StringComparison.InvariantCultureIgnoreCase)) {
return;
}
base.Selected (pDialogViewController, pTableView, pIndexPath);
// Is there an event mapped to our OnSelected event handler?
var selected = OnSelected;
// If yes, fire it.
if (selected != null) {
selected (this, EventArgs.Empty);
}
// Close the dialog.
pDialogViewController.DeactivateController(true);
}
static public event EventHandler<EventArgs> OnSelected;
}