2011-11-28 15 views
8

Mam okno dialogowe utworzone przez MonoTouch.Dialog. Jest to lista lekarzy w grupie radiowej:MonoTouch.Dialog: Odpowiadanie na wybór w grupie RadioGroup

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

Chciałbym zaktualizować Element w kodzie raz Lekarz został wybrany. Jaki jest najlepszy sposób, aby otrzymać powiadomienie, że wybrano RadioElement?

Odpowiedz

18

Stwórz własne RadioElement jak:

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

uwaga: nie używać statycznego zdarzenie jeśli chcesz mieć więcej niż jedną grupę radiowy

Następnie utwórz RootElement które używają tego nowego typu jak:

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]

Tutaj jest bardziej nowoczesna wersja tego 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); 
    } 
} 
+2

Byłby to wspaniały dodatek do MT.Dialog jak w wielu line-of-biznesowych aplikacji, wybór jednej dziedzinie wpływa na drugie. Dzięki za wspaniałą odpowiedź! –

Powiązane problemy