How to pass data to MonoTouch.Dialog delegates?

Given the following code, how can I transfer data from "First Name", "Last Name", etc. in my BookASession.SendMessage(); method BookASession.SendMessage(); ?

 RootElement CreateBookASessionRoot() { return new RootElement("Book a Session") { new Section() { new EntryElement("First Name", "First Name", ""), new EntryElement("Last Name", "Last Name", ""), new EntryElement("Email", "Email", "") { KeyboardType = UIKeyboardType.EmailAddress }, new DateElement("Event Date", DateTime.Now), new RootElement ("Type of Shoot", new RadioGroup (0)){ new Section (){ new RadioElement("Wedding"), new RadioElement("Portrait"), new RadioElement("Boudoir"), new RadioElement("Other") } } , new EntryElement("Message", "Message", "") } , new Section () { new StringElement("Send", delegate { BookASession.SendMessage(); } ) } }; } 
+4
source share
2 answers

The way I like it is with links to my input elements. That way, I can easily get my input values ​​without having to search the entire tree of elements. I do this by encapsulating the creation logic for a particular screen in a separate clan, for example:

 public class BookASessionScreen { private RootElement _root = null; private EntryElement _firstName = null; private EntryElement _lastName = null; private EntryElement _email = null; private DateElement _date = null; private RootElement _typeOfShoot = null; private EntryElement _message = null; private void CreateRoot() { _firstName = new EntryElement("First Name", "First Name", ""); _lastName = _firstName = new EntryElement("First Name", "First Name", ""); _email = new EntryElement("Email", "Email", "") { KeyboardType = UIKeyboardType.EmailAddress }; _date = new DateElement("Event Date", DateTime.Now); _typeOfShoot = new RootElement ("Type of Shoot", new RadioGroup (0)){ new Section () { new RadioElement("Wedding"), new RadioElement("Portrait"), new RadioElement("Boudoir"), new RadioElement("Other") } }; _message = new EntryElement("Message", "Message", ""); _root = new RootElement("Book a Session") { new Section() { _firstName, _lastName, _email, _date, _typeOfShoot, _message } , new Section () { new StringElement("Send", delegate { //BookASession.SendMessage(_firstName.Value, _lastName.Value, ...) }) } }; } public RootElement Root { get { if (_root == null) { CreateRoot(); } return _root; } } } 

In addition, you may need to separate the form processing logic from the fact that the class exposes the event, for example:

1 - Create a class that will contain the event data, expanding EventArgs:

 public class BookASessionArgs : EventArgs { public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } } 

2 - Announce your event in BookASessionScreen:

 public event EventHandler BookASession; 

3 - Empty the event in your delegate

 if (BookASession != null) { BookASession(this, new BookASessionArgs() { FirstName = _firstName.Value, LastName = _lastName.Value //.. }); } 
+7
source

The problem with value properties not receiving an update can be resolved by calling the ResignFirstResponder method on the element with focus. I assume this is something specific to iOS.

+1
source

Source: https://habr.com/ru/post/1385043/


All Articles