Send mail and call from listview.

LoLeHLoLeH DKMember ✭✭
edited September 2017 in Cross Platform with Xamarin

How do i make my "OnEmailTapped" and "TlfnrTapped" not static?

I want to make a contact list with Names, Email adresses and Phone numbers in it. So i can send mails and call from my listview, but right now i can only call "12345678" when i tap on a phonenumber. i want to make it so i call to the phone number i tap on. The same with email, i want to send a mail to the mail adress i tap and not "[email protected]". is there a way to do that?

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 string Adresse { get; set; }
    public string Billed { get; set; }

    public List<Kontakter> GetKontakter()
    {
        List<Kontakter> kontakter = new List<Kontakter>
    {
        new Kontakter
        {
            Fuldenavn = "Anders (ANSE)",
            Email = "[email protected]",
            Tlfnr = 12345678,
        },
        new Kontakter
        {
            Fuldenavn = "Anja (ANBI)",
            Email = "[email protected]",
            Tlfnr = 87654321,
        },
        new Kontakter
        {
            Fuldenavn = "Benn (BMR)",
            Email = "[email protected]",
            Tlfnr = 12876534,
        },
        new Kontakter
        {
            Fuldenavn = "Christian (CBE)",
            Email = "[email protected]",
            Tlfnr = 18273645,
        },
    };
        return kontakter;
    }
}
}

My Main.cs:

using Plugin.Messaging;
using System;
using System.Linq;
using Xamarin.Forms;

namespace App3
{
public partial class MainPage : ContentPage
{
    KontaktlisteView vm;

    public MainPage()
    {
        InitializeComponent();
        vm = new KontaktlisteView();
        NameslistView.ItemsSource = vm.Kontakter;
    }

    private async void Kontakter_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        ((ListView)sender).SelectedItem = null;
        var Kontakter = e.Item as Kontakter;

        await DisplayAlert("Kontakt", "\nName: " + Kontakter.Fuldenavn + "\nTelefon: " + Kontakter.Tlfnr + "\nEmail: " + Kontakter.Email, "Luk");
    }

    private void MainSearchBar_SearchButtonPressed(object sender, EventArgs e)
    {
        var keyword = MainSearchBar.Text;
        NameslistView.ItemsSource = vm.Kontakter.Where(obj => (obj.Fuldenavn.Contains(keyword) || obj.Tlfnr.ToString().Contains(keyword)));
    }
    private void MainSearchBar_TextChanged(object sender, TextChangedEventArgs e)
    {
        NameslistView.ItemsSource = vm.Kontakter.Where(obj => (obj.Fuldenavn.Contains(e.NewTextValue) || obj.Tlfnr.ToString().Contains(e.NewTextValue)));
    }

    private void OnEmailTapped(object sender, EventArgs e)
    {
        var emailMessenger = CrossMessaging.Current.EmailMessenger;
        if (emailMessenger.CanSendEmail)

            emailMessenger.SendEmail("[email protected]");
    }

    private void TlfnrTapped(object sender, EventArgs e)
    {
        var phoneDialer = CrossMessaging.Current.PhoneDialer;
        if (phoneDialer.CanMakePhoneCall)
            phoneDialer.MakePhoneCall("12345678");
    }
}
}

My Main.xmal.cs:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App3"
         x:Class="App3.MainPage">

<StackLayout>
<SearchBar x:Name="MainSearchBar" Placeholder="Søg" SearchButtonPressed="MainSearchBar_SearchButtonPressed" TextChanged="MainSearchBar_TextChanged"/>
<ListView x:Name="NameslistView" HasUnevenRows="True" ItemTapped="Kontakter_ItemTapped" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Vertical" >
                        <Grid HeightRequest="5" BackgroundColor="White"  />
                        <StackLayout Orientation="Horizontal" BackgroundColor="LightSkyBlue" >
                            <Image Source="{Binding Billed}" />
                            <StackLayout Orientation="Vertical">

                                <Label Text="{Binding Fuldenavn}" TextColor="Black" FontSize="Large" />

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

                                    <Label.GestureRecognizers>
                                        <TapGestureRecognizer Tapped="TlfnrTapped"/>
                                    </Label.GestureRecognizers>
                                </StackLayout>

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

                                    <Label.GestureRecognizers>
                                        <TapGestureRecognizer Tapped="OnEmailTapped"/>
                                    </Label.GestureRecognizers>
                                </StackLayout>



                            </StackLayout>
                        </StackLayout>
                    </StackLayout >
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout >

</ContentPage>

My ContactsView:

using System.Collections.Generic;

namespace App3
{
public class KontaktlisteView
{
    public List<Kontakter> Kontakter { get; set; }
    public static object SelectedItem { get; internal set; }

    public KontaktlisteView()
    {
        Kontakter = new Kontakter
            ().GetKontakter();
    }
}
}

My App.CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xamarin.Forms;

namespace App3
{
public partial class App : Application
{
    public App ()
    {
        InitializeComponent();

        MainPage = new App3.MainPage();
    }

    protected override void OnStart ()
    {
        // Handle when your app starts
    }

    protected override void OnSleep ()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume ()
    {
        // Handle when your app resumes
    }
}
}
Sign In or Register to comment.