0

i have a list of contacts and i want to send a mail to the mail adress i tap. but right now i can only send a mail to a static adress (StaticMail@gmail.com). how do i send a mail to the adress i tap on my listview?

my contacts:

using System.Collections.Generic;

namespace App3
{
    public class Kontakter
    {
        public string Fuldenavn { get; set; }
        public int Tlfnr { get; set; }
        public string Email { get; set; }

        public List<Kontakter> GetKontakter()
        {
            List<Kontakter> kontakter = new List<Kontakter>
        {
            new Kontakter
            {
                Fuldenavn = "bacon (ANSE)",
                Email = "Random@gmail.com",
                Tlfnr = 12345678,
            },
            new Kontakter
            {
                Fuldenavn = "Anja (ANBI)",
                Email = "Random@hotmail.dk",
                Tlfnr = 87654321,
            },
            new Kontakter
            {
                Fuldenavn = "Benn (BMR)",
                Email = "Random@hotmail.com",
                Tlfnr = 12876534,
            },
            new Kontakter
            {
                Fuldenavn = "Christian (CBE)",
                Email = "Nothing@gmail.com",
                Tlfnr = 18273645,
            },
        };
            return kontakter;
        }
    }
}

my function:

private void OnEmailTapped(object sender, EventArgs e)
{
    var emailMessenger = CrossMessaging.Current.EmailMessenger;
    if (emailMessenger.CanSendEmail)
    {
        emailMessenger.SendEmail("StaticMail@gmail.com");
    }
}

my XMAL.CS:

<StackLayout>
<Label Text="{Binding Email}" TextColor="Black" FontSize="Medium" />

<Label.GestureRecognizers>
 <TapGestureRecognizer Tapped="OnEmailTapped"/>
</Label.GestureRecognizers>
</StackLayout>
  • If the email address is correctly bound to the Label - you could try this _"emailMessenger.SendEmail(((Label)sender).Text);"_ – PaulF Sep 19 '17 at 12:18
  • just crashes when i tap on any mail https://gyazo.com/7e1b98d49d382486ecb3d2fff95cfc58?token=2dd0eeb2f3a4bffb24254242e3722590 @PaulF – Noob coder Sep 19 '17 at 12:33
  • Can you send the code i need to paste in? @benichka – Noob coder Sep 19 '17 at 12:35
  • I removed my comment: it was wrong, I read it too quickly. However, it's kind of strange that your application crashes. Do you run it in debug? What's the exception? – benichka Sep 19 '17 at 12:40
  • Not strange - I missed the fact the event is part of the TapGestureRecognizer - see this to find the Label object https://stackoverflow.com/questions/21573036/tap-gesture-recognizer-which-object-was-tapped – PaulF Sep 19 '17 at 12:43
  • yes i run in debug. and it only crashes when i use this code @PaulF `emailMessenger.SendEmail(((Label)sender).Text);` – Noob coder Sep 19 '17 at 12:44
  • use try/catch to capture the exception – Jason Sep 19 '17 at 12:51
  • You could give the Label a name _ – PaulF Sep 19 '17 at 12:51
  • now i called my label `x:Name="EmailLabel"` and paste this `emailMessenger.SendEmail(EmailLabel.Text);` but i get Error CS0103 The name 'EmailLabel' does not exist in the current context @PaulF – Noob coder Sep 19 '17 at 13:01
  • Maybe this will work - binding to the Command & CommandParameter properties rather than subscribing to the Tapped event. https://forums.xamarin.com/discussion/84658/how-to-pass-parameters-to-tapped-event – PaulF Sep 19 '17 at 14:04

0 Answers0