c# - 如何将数据传递给 MonoTouch.Dialog 的委托(delegate)?

标签 c# ios xamarin.ios monotouch.dialog

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

最佳答案

我喜欢通过保留对我的输入元素的引用来完成此操作。这样我就可以轻松获取他们的输入值,而无需搜索整个元素树。为此,我将特定屏幕的创建逻辑封装在一个单独的类中,如下所示:

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 - 在您的委托(delegate)中触发事件

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

关于c# - 如何将数据传递给 MonoTouch.Dialog 的委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8410090/

相关文章:

xamarin.ios - 如何让 Fody/PropertyChanged 在 Xamarin Studio 的 iOS 项目中工作?

c# - 将 UDP 封装在 TCP 协议(protocol)中

ios - 如何在不同的 View Controller 中拥有相同的视频实例?

ios - Inout 参数不允许互相使用别名

ios - 对包含 NSDate 对象的 NSArray 进行排序

.net - 为什么 Apple 允许在 iPhone 上使用 .NET 而不允许使用 Flash?

java - 警告 Xamarin android - 未提供 -tsa 或 -tsacert 并且此 jar 没有时间戳

c# - 如何使用 Json.NET 对特定属性进行驼峰式大小写?

c# - .Net Core Automapper 缺少类型映射配置或不受支持的映射

c# - Mvvm在窗口中拖动网格