9

我有一个由 MonoTouch.Dialog 创建的对话框。广播组中有一个医生列表:

    Section secDr = new Section ("Dr. Details") {
       new RootElement ("Name", rdoDrNames){
          secDrNames
    }

Element一旦选择了医生,我希望更新代码中的 an 。RadioElement通知已选择a 的最佳方式是什么?

4

1 回答 1

18

创建自己的RadioElement喜欢:

class MyRadioElement : RadioElement {
    public MyRadioElement (string s) : base (s) {}

    public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
    {
        base.Selected (dvc, tableView, path);
        var selected = OnSelected;
        if (selected != null)
            selected (this, EventArgs.Empty);
    }

    static public event EventHandler<EventArgs> OnSelected;
}

注意:如果您想拥有多个广播组,请不要使用静态事件

然后创建一个RootElement使用这种新类型的,如:

    RootElement CreateRoot ()
    {
        StringElement se = new StringElement (String.Empty);
        MyRadioElement.OnSelected += delegate(object sender, EventArgs e) {
            se.Caption = (sender as MyRadioElement).Caption;
            var root = se.GetImmediateRootElement ();
            root.Reload (se, UITableViewRowAnimation.Fade);
        };
        return new RootElement (String.Empty, new RadioGroup (0)) {
            new Section ("Dr. Who ?") {
                new MyRadioElement ("Dr. House"),
                new MyRadioElement ("Dr. Zhivago"),
                new MyRadioElement ("Dr. Moreau")
            },
            new Section ("Winner") {
                se
            }
        };
    }

[更新]

这是此 RadioElement 的更现代版本:

public class DebugRadioElement : RadioElement {
    Action<DebugRadioElement, EventArgs> onCLick;

    public DebugRadioElement (string s, Action<DebugRadioElement, EventArgs> onCLick) : base (s) {
        this.onCLick = onCLick;
    }

    public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
    {
        base.Selected (dvc, tableView, path);
        var selected = onCLick;
        if (selected != null)
        selected (this, EventArgs.Empty);
    }
}
于 2011-11-28T23:14:01.393 回答