MonoTouch.Dialog如何从对话框中获取数据

时间:2012-03-26 10:29:27

标签: xamarin.ios monotouch.dialog

我是MonoTouch和MonoTouch.Dialog的初学者。 我正在尝试使用MT 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)

2 个答案:

答案 0 :(得分:3)

非常简单,将中间值分配给变量:

Section s;
SomeElement e;

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

答案 1 :(得分: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();

    }
}