如何传递数据到MonoTouch.Dialog的代表? [英] How can I pass data into MonoTouch.Dialog's delegates?
问题描述
考虑下面的代码,我怎么能传递从名,姓等资料到我的方法 BookASession.SendMessage();
rootElement的CreateBookASessionRoot()
{
返回新rootElement的(尚书会话){
新科(){
新EntryElement(名,名,),
新EntryElement(姓,姓氏,),
新EntryElement(电子邮件,电子邮件,){键盘类型= UIKeyboardType.EmailAddress}
新DateElement(事件发生日,DateTime.Now),
新rootElement的(类型拍摄的新RadioGroup中(0)){
新科(){
新的放射性元素(婚礼),
新的放射性元素(纵向),
新的放射性元素(闺怨),
新的放射性元素(其他)
}
},
新EntryElement(信息,信息,)
}
新科(){
新StringElement(发送,代表{BookASession.SendMessage(); })
}
};
}
我想实现的方式这是通过保持引用我的输入元素。这样我可以很容易,而不必通过整个元素树进行搜索获取自己的输入值。我在一个单独的封装化酶创建逻辑特定的屏幕,像这样这样做:
公共类BookASessionScreen
{
私人rootElement的_root = NULL;
私人EntryElement _FirstName = NULL;
私人EntryElement _lastName = NULL;
私人EntryElement _EMAIL = NULL;
私人DateElement _date = NULL;
私人rootElement的_typeOfShoot = NULL;
私人EntryElement _message = NULL;
私人无效CreateRoot()
{
_FirstName =新EntryElement(名,名,);
_lastName = _FirstName =新EntryElement(名,名,);
_EMAIL =新EntryElement(电子邮件,电子邮件,){键盘类型= UIKeyboardType.EmailAddress};
_date =新DateElement(事件发生日,DateTime.Now);
_typeOfShoot =新rootElement的(笋类型,新的RadioGroup中(0)){
新科(){
新的放射性元素(婚礼),
新的放射性元素( 纵向),
新的放射性元素(闺怨),
新的放射性元素(其他)
}
};
_message =新EntryElement(信息,信息,);
_root =新rootElement的(书会话){
新科(){
_FirstName,
_lastName,
_EMAIL,
_date,
_typeOfShoot,
_message
},
新科(){
新StringElement(发送,代表{
// BookASession。 SendMessage函数(_firstName.Value,_lastName.Value,...)
})
}
};
}
公共rootElement的根
{
获得{
如果(_root == NULL){
CreateRoot();
}
返回_root;
}
}
}
此外,您可能希望通过让类暴露的事件,这样的分离形式的处理逻辑:
1 - 创建一个类,将持有事件数据,延长EventArgs的:
公共类BookASessionArgs:EventArgs的
{
公共字符串名字
{
搞定;
组;
}
公共字符串姓氏
{
获得;
组;
}
公共字符串的电子邮件
{
获得;
组;
}
}
2 - 在BookASessionScreen声明您的活动:
公共事件的EventHandler BookASession;
3 - 消防在委托事件
如果(BookASession!= NULL){
BookASession(这一点,新BookASessionArgs(){
名字= _firstName.Value,
姓氏= _lastName.Value
// ..
});
}
Given the following code, how can I pass data from "First Name", "Last Name", etc. into my 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(); } )
}
};
}
The way i like to accomplish this is by keeping references to my input elements. This way i can easily fetch their input values without having to search through the entire element tree. I'm doing so by encapsulating the creation logic for a particular screen in a separate clase, like this:
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;
}
}
}
Also, you may want to decouple the form processing logic by having the class expose an event, like this:
1 - Create a class that will hold the event data, extending EventArgs:
public class BookASessionArgs : EventArgs
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public string Email
{
get;
set;
}
}
2 - Declare your event in BookASessionScreen:
public event EventHandler BookASession;
3 - Fire the event in your delegate
if (BookASession != null) {
BookASession(this, new BookASessionArgs() {
FirstName = _firstName.Value,
LastName = _lastName.Value
//..
});
}
这篇关于如何传递数据到MonoTouch.Dialog的代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!