4

给定以下代码,如何将“名字”、“姓氏”等数据传递到我的方法中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

2 回答 2

7

我喜欢实现这一点的方法是保留对我的输入元素的引用。这样我就可以轻松地获取它们的输入值,而无需搜索整个元素树。我这样做是通过将特定屏幕的创建逻辑封装在一个单独的类中来实现的,如下所示:

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

此外,您可能希望通过让类公开事件来解耦表单处理逻辑,如下所示:

1 - 创建一个将保存事件数据的类,扩展 EventArgs:

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

    public string LastName
    {
        get;
        set;
    }

    public string Email
    {
        get;
        set;
    }
}

2 - 在 BookASessionScreen 中声明您的活动:

public event EventHandler BookASession;

3 - 在您的委托中触发事件

if (BookASession != null) {
    BookASession(this, new BookASessionArgs() {
        FirstName = _firstName.Value,
        LastName = _lastName.Value
        //..
    });
}
于 2011-12-07T09:51:06.367 回答
1

值属性未更新的问题可以通过在具有焦点的元素上调用方法 ResignFirstResponder 来解决。我想这是 iOS 特有的。

于 2012-03-21T10:26:27.800 回答