如何制作我的" OnEmailTapped"和" TlfnrTapped"不是静止的?
我想制作一个包含姓名,电子邮件地址和电话号码的联系人列表。所以我可以从我的列表视图发送邮件和电话,但是现在我只能打电话给#34; 12345678"当我点击一个电话号码。我想这样做,所以我打电话给我点击的电话号码。与电子邮件相同,我想发送邮件到我点击的邮件地址而不是" Something@gmail.com"。有没有办法做到这一点?
我的联系人:
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 = "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 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("Something@gmail.com");
}
private void TlfnrTapped(object sender, EventArgs e)
{
var phoneDialer = CrossMessaging.Current.PhoneDialer;
if (phoneDialer.CanMakePhoneCall)
phoneDialer.MakePhoneCall("12345678");
}
}
}
我的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>
我的联系人视图:
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();
}
}
}
我的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
}
}
}
答案 0 :(得分:0)
如果您的绑定是正确的:
您可以点击电子邮件地址并使用以下方式发送电子邮件:
emailMessenger.SendEmail(((Label)sender).Text);
与电话号码相同:
phoneDialer.MakePhoneCall(((Label)sender).Text);