Xamarin Forms: How to fetch the latest contacts of phone after adding a number to phonebook?

Sreejith Sree 1,251 Reputation points
2021-06-30T13:46:17.627+00:00

I have referred to this blog for listing the phone contacts. Also, I have implemented adding contacts to the phonebook using DependencyService as per this thread.

My problem is after adding a contact to the device phone book I need to fetch the entire latest contacts. Also, I need to show the new contact on the contact listview.

For reference, I have created a sample project and uploaded it here. In this sample first of all I am listing the phone contacts with the Add New option on the top. If we tap Add New, a new page will open with the Add to Contact option and entry for phone number. Click the Add to Contact option after entering a phone number, then the device phonebook page will show with the phone number entered.

At this stage, the user may or may not save that number to the device phonebook. So when the user resuming to the App I need to fetch the entire contacts and check the phone number is added or not to the device phone book. If contact added I will hide the Add to Contact option else I will show that option again. At the same time when the user going back to the contact list, I need to show the newly added contact over there.

For this, I have added a message on App.xaml.cs and Subscribe it on AddContactPage.

App.xaml.cs

protected override void OnResume()
{
    // Handle when your app resumes
    MessagingCenter.Send(this, "isContactAdded");
}

AddContactPage

MessagingCenter.Subscribe<App>(this, "isContactAdded", (sender) =>
{
    //How I can fetch the entire latest contacts here
    //After fetching conatcts only I can check the new phone number is added to the device phonebook
});

The contacts are passing as an argument from MainActivity to App.xaml.cs. So I don't know how to fetch it directly. Is there any way to fetch all contacts on this page?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,261 Reputation points Microsoft Vendor
    2021-07-01T07:56:58.883+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you want to fetch the entire latest contacts.

    First, open your App.xaml.cs, Use IContactsService contactsService; to get all of contacts, here is all of App.xaml.cs code.

    Then open your MainPage.xaml.cs, We can send contacts(did not add new records) to AddContactPage. Here is all of code about MainPage.xaml.cs.

    In the end, open AddContactPage.xaml.cs. Compare two list(add new record and not add new record list), we can fetch the entire latest contacts in MessagingCenter.Subscribe method.

    Due to this site issue, I cannot post code, So I update my code with .txt file.

    110913-mycode.txt

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful