2011-11-28 40 views
8

我有一個由MonoTouch.Dialog創建的對話框。有醫生的無線電組的列表:MonoTouch.Dialog:響應RadioGroup的選擇

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

我要更新一次,醫生已經選擇了代碼Element。被通知選擇RadioElement的最佳方式是什麼?

回答

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]

下面是該放射性元素的更現代的版本:

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); 
    } 
} 
+2

這將是一個偉大的除了MT.Dialog在許多業務線 - 應用程序,一個領域的選擇會影響另一個。感謝您的優秀回答! –

相關問題