MonoTouch.Dialog how to get data from dialog

2019-05-24 23:56发布

问题:

I'm a beginner in MonoTouch and MonoTouch.Dialog. I am trying to use MT Dialog but I cannot understand how to get data in and out.

Let's say I have Event class:

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

And I want to edit it using this dialog definition:

        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")
                    }

                }
            },

How can I pass data to and from this form? (using low-level API not Reflection which supports binding)

回答1:

Very easy, assign the intermediate values to variables:

Section s;
SomeElement e;

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


回答2:

You can do something like this:

    //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();

    }
}