2011-11-28 25 views
8

Tengo un Diálogo creado por MonoTouch.Dialog. Hay una lista de médicos en un grupo de radio:MonoTouch.Dialog: Respondiendo a una Opción de RadioGroup

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

Deseo actualizar un Element en el código una vez que un médico ha sido elegido. ¿Cuál es la mejor manera de recibir notificación de que se ha seleccionado RadioElement?

Respuesta

18

Crear su propio RadioElement como:

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: no utilice un evento estática, si usted quiere tener más de un grupo de radio

A continuación, crear una RootElement que utilizan este nuevo tipo , como:

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

[ACTUALIZACIÓN]

Aquí es una versión más moderna de este radioelementos:

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

Esto sería una gran adición a MT.Dialog como en muchas de las aplicaciones de línea de negocio, la elección de un campo afecta a otro. Gracias por tu excelente respuesta! –

Cuestiones relacionadas