MonoTouch.Dialog:响应RadioGroup选择

时间:2011-11-28 21:06:48

标签: c# ios xamarin.ios radio-button monotouch.dialog

我有一个由MonoTouch.Dialog创建的Dialog。收音机组中有一份医生名单:

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

我希望在选择Doctor后更新代码中的Element。通知已选择RadioElement的最佳方式是什么?

1 个答案:

答案 0 :(得分: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
            }
        };
    }

[UPDATE]

以下是此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);
    }
}