I use this code for calling in Xamarin.
enter code here var phoneDialer = CrossMessaging.Current.PhoneDialer;
if (phoneDialer.CanMakePhoneCall)
phoneDialer.MakePhoneCall("?");
I have list with contacts. I want that, if I click på phone number, then it is calling direct to person I want. If I write some number in ? place, function works, but phone number is the same for all contacts. What I need to write in ? place, at that find and change number, if I click on another person?
This is my kontaktlist:
public partial class Kontaktliste : ContentPage
{
KontaktlisteView vm;
public int Tlfnr { get; private set; }
public Kontaktliste()
{
InitializeComponent();
vm = new KontaktlisteView();
NameslistView.ItemsSource = vm.Kontakter;
//Order the contacts
var sorted = vm.Kontakter.OrderBy(x => x.Fødselsdage)
.ToList();
//Set the ItemsSource with the ordered contacts
NameslistView.ItemsSource = sorted;
}
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 + "\nAdresse: " + Kontakter.Adresse + "\nFødselsdage: " + Kontakter.Fødselsdage, "Luk");
}
//Search button
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)
{
if (e.NewTextValue == string.Empty)
{
NameslistView.ItemsSource = vm.Kontakter.Where(name => (name.Fuldenavn.Contains("")));
}
}
//Ring når klikke på telefon nummer
public void OnTelefonTapped(object sender, EventArgs e)
{
var phoneDialer = CrossMessaging.Current.PhoneDialer;
if (phoneDialer.CanMakePhoneCall)
phoneDialer.MakePhoneCall("");
}
and contacts
public class Kontakter
{
public static List<Kontakter> ItemsSource { get; internal set; }
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 DateTime Fødselsdage { get; set; }
public List<Kontakter> GetKontakter()
{
List<Kontakter> kontakter = new List<Kontakter>()
{
new Kontakter ()
{
Fuldenavn = Name,
Tlfnr =1234567
Email = [email protected]
Adresse =
Billed =
Fødselsdage=new DateTime()
},
And
<ScrollView>
<StackLayout>
<SearchBar x:Name="MainSearchBar" Placeholder="Søg" SearchButtonPressed="MainSearchBar_SearchButtonPressed" TextChanged="MainSearchBar_TextChanged"/>
<ListView x:Name="NameslistView" HasUnevenRows="True" ItemTapped="Kontakter_ItemTapped"
ItemsSource="{Binding Kontakter}" >
<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="OnTelefonTapped">
</TapGestureRecognizer>
</Label.GestureRecognizers>
</StackLayout>
<StackLayout>
<Label Text="{Binding Email}" TextColor="Black" FontSize="15"></Label>
<Label.GestureRecognizers>
<TapGestureRecognizer
Tapped="OnEmailTapped">
</TapGestureRecognizer>
</Label.GestureRecognizers>
</StackLayout>
</StackLayout>
</StackLayout>
</StackLayout >
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout >
</ScrollView>
since your TapGestureRecognizer is attached to a Label, you should be able to do this
public void OnTelefonTapped(object sender, EventArgs e)
{
var source = (Label)sender;
var phone = source.Text;
var phoneDialer = CrossMessaging.Current.PhoneDialer;
if (phoneDialer.CanMakePhoneCall)
phoneDialer.MakePhoneCall(phone);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments