MonoTouch.Dialog:响应RadioGroup选择

Ian*_*ink 9 c# radio-button xamarin.ios ios monotouch.dialog

我有一个由MonoTouch.Dialog创建的Dialog.无线电组中有一份医生名单:

    Section secDr = new Section ("Dr. Details") {
       new RootElement ("Name", rdoDrNames){
          secDrNames
    }
Run Code Online (Sandbox Code Playgroud)

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

pou*_*pou 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;
}
Run Code Online (Sandbox Code Playgroud)

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

然后创建一个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
            }
        };
    }
Run Code Online (Sandbox Code Playgroud)

[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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这将是MT.Dialog的一个很好的补充,因为在许多业务线应用程序中,一个字段的选择会影响另一个.谢谢你的出色答案! (3认同)