【问题标题】:StyledStringElement 'Tapped' eventStyledStringElement 'Tapped' 事件
【发布时间】:2013-05-02 12:51:25
【问题描述】:

所以,当用户单击“StyledStringElement”时,我试图打开一个电子邮件界面 - 为此,我一直在调用点击事件,但我得到了错误 -

“错误 CS1502:最佳重载方法匹配 `MonoTouch.Dialog.Section.Add(MonoTouch.Dialog.Element)' 有一些 无效参数 (CS1502)"

“错误 CS1503:参数 #1' cannot convertvoid' 表达式要键入 `MonoTouch.Dialog.Element' (CS1503)"

我使用的代码是-

        section.Add(new StyledStringElement("Contact Email",item.Email) {
            BackgroundColor=UIColor.FromRGB(71,165,209),
            TextColor=UIColor.White,
            DetailColor=UIColor.White,
        }.Tapped += delegate {
            MFMailComposeViewController email = new MFMailComposeViewController();
            this.NavigationController.PresentViewController(email,true,null);
    });

是什么导致了这个错误,我该如何解决?

【问题讨论】:

    标签: c# ios mono xamarin.ios monodevelop


    【解决方案1】:

    需要单独初始化“StyledStringElement”

    例如:

    var style = new StyledStringElement("Contact Email",item.Email) {
                BackgroundColor=UIColor.FromRGB(71,165,209),
                TextColor=UIColor.White,
                DetailColor=UIColor.White,
            };
    
    style.Tapped += delegate {
                MFMailComposeViewController email = new MFMailComposeViewController();
                this.NavigationController.PresentViewController(email,true,null);
        };
    
    section.Add(style);
    

    【讨论】:

      【解决方案2】:

      new X().SomeEvent += Handler 的返回值为void,因此您不能在您的部分中添加它。

      不幸的是,C# 官方** 不支持在对象初始化器 (Assigning events in object initializer) 中分配事件,所以你也不能这样做:

      new X() {
          SomeEvent += Handler,
      };
      

      如果你还想同时实例化和附加,你能来的最接近的是

      StyleStringElement style;
      section.Add(style = new StyledStringElement("Contact Email",item.Email) {
              BackgroundColor=UIColor.FromRGB(71,165,209),
              TextColor=UIColor.White,
              DetailColor=UIColor.White,
          });
      
      style.Tapped += delegate {
              MFMailComposeViewController email = new MFMailComposeViewController();
              this.NavigationController.PresentViewController(email,true,null);
      };
      

      ** 当我正式说的时候,是因为我记得有些人让它在 mono c# 编译器的某些分支中工作。

      【讨论】:

        猜你喜欢
        • 2014-05-31
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2014-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多