2012-03-26 4 views
2

Я новичок в MonoTouch и MonoTouch.Dialog. Я пытаюсь использовать MT Dialog, но я не могу понять, как получить данные и выйти.MonoTouch.Dialog как получить данные из диалога

Скажем, у меня есть класс событий:

class Event { 
bool type {get;set;} 
string name {get;set;} 
} 

И я хочу, чтобы изменить его с помощью этого диалогового определение:

 return new RootElement ("Event Form") { 

     // string element 
      new Section ("Information"){ 
       new EntryElement ("Name", "Name of event", ""), 
       new RootElement ("Type", new RadioGroup (0)){ 
        new Section(){ 
         new RadioElement ("Concert"), 
         new RadioElement ("Movie"), 
         new RadioElement ("Exhibition"), 
         new RadioElement ("Sport") 
        } 

       } 
      }, 

Как я могу передать данные и от этой формы? (С использованием низкоуровневого API не Reflection, который поддерживает связывание)

ответ

0

Вы можете сделать что-то вроде этого:

//custom class to get the Tapped event to work in a RadioElement 
    class OptionsRadioElement: RadioElement 
    { 
      public OptionsRadioElement(string caption, NSAction tapped): base(caption) 
      { 
       Tapped += tapped; 
      } 
    } 

//Custom Controller 
public class MyController: DialogViewController 
{ 
    private readonly RadioGroup optionsGroup; 
    private readonly EntryElement nameField; 



    public MyController(): base(null) 
    { 
     //Note the inline assignements to the fields 
     Root = new RootElement ("Event Form") { 
      new Section ("Information"){ 
      nameField = new EntryElement ("Name", "Name of event", ""), 
      new RootElement ("Type", optionsGroup = new RadioGroup (0)){ 
       new Section(){ 
        new OptionsRadioElement("Concert", OptionSelected), 
        new OptionsRadioElement("Movie", OptionSelected), 
        new OptionsRadioElement("Exhibition", OptionSelected), 
        new OptionsRadioElement("Sport", OptionSelected) 
       } 

      } 
     }; 
    } 

    private void OptionSelected() 
    { 
     Console.WriteLine("Selected {0}", optionsGroup.Selected); 
    } 


    public void SetData(MyData data) 
    { 
      switch(data.Option) 
      { 
       case "Concert: 
        optionsGroup.Selected = 0; 
        break; 
       case "Movie": 
        optionsGroup.Selected = 1; 
        break; 
        //And so on.... 
       default: 
        optionsGroup.Selected = 0; 
        break; 
      } 
      nameField.Value = data.Name; 
      ReloadData(); 

    } 
} 
3

Очень легко, назначить промежуточные значения переменных:

Section s; 
SomeElement e; 

return new RootElement ("Foo") { 
    (s = new Section ("...") { 
     (e = new StringElement (...)) 
    }) 
}; 
+0

Что делать, если вы рендеринг элементов из базы данных, как в http://stackoverflow.com/questions/14866188/monotouch-dialog-generate-from-db-and-retain-values? – BRogers

Смежные вопросы