MonoTouch.Dialog: Responding to a RadioGroup Choice

橙三吉。 提交于 2019-12-30 00:04:27

问题


I have a Dialog created by MonoTouch.Dialog. There is a list of Doctors in a radio group:

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

I wish to update an Element in the code once a Doctor has been chosen. What's the best way to be notified that a RadioElement has been selected?


回答1:


Create your own RadioElement like:

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;
}

note: do not use a static event if you want to have more than one radio group

Then create a RootElement that use this new type, like:

    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]

Here is a more modern version of this 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);
    }
}


来源:https://stackoverflow.com/questions/8302306/monotouch-dialog-responding-to-a-radiogroup-choice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!