1
votes

So, I am attempting to open an email interface when a user clicks on the 'StyledStringElement' - To do this I have been calling the tapped event yet I have gained the error -

"Error CS1502: The best overloaded method match for `MonoTouch.Dialog.Section.Add(MonoTouch.Dialog.Element)' has some invalid arguments (CS1502)"

and

"Error CS1503: Argument #1' cannot convertvoid' expression to type `MonoTouch.Dialog.Element' (CS1503)"

The code I am using is -

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

What is causing this error and how can I fix it?

2

2 Answers

2
votes

You need to initialize the "StyledStringElement" separately

For example:

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);
0
votes

The return value for new X().SomeEvent += Handler is void, so you can't add that in your section.

Unfortunately, C# officially** doesn't support assigning events in object initialiser (Assigning events in object initializer), so you can't do this neither:

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

If you still want to instantiate and attach at the same time, the closest you can come is

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

** when I say officially, it's because I remember some people got it to work in some branches of the mono c# compiler.