2011-11-28 14 views
8

Ho una finestra di dialogo creata da MonoTouch.Dialog. C'è una lista di medici in un gruppo di pulsanti:.MonoTouch.Dialog: risposta a una scelta di gruppo radio

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

desidero aggiornare un Element nel codice una volta che un medico è stato scelto. Qual è il modo migliore per essere avvisati che è stato selezionato un RadioElement?

risposta

18

Crea il tuo RadioElement come:

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

nota: non utilizzare un evento statico se si vuole avere più di un gruppo Radio

Quindi creare un RootElement che utilizzano questo nuovo tipo , come:

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]

Ecco una versione più moderna di questo radioelemento:

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

Questa sarebbe una grande aggiunta a MT.Dialog come in molte applicazioni line-of-business, la scelta di un campo colpisce l'altro. Grazie per la tua eccellente risposta! –

Problemi correlati