Publicly declare Contacts

Jassim Al Rahma 1,521 Reputation points
2023-01-19T22:42:25.9966667+00:00

Hi,

How can I publicly declare the following:

var contacts = await Microsoft.Maui.ApplicationModel.Communication.Contacts.GetAllAsync();

something like this:

Contacts contacts;

contacts = await Microsoft.Maui.ApplicationModel.Communication.Contacts.GetAllAsync();

Thanks,

Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,854 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,146 Reputation points Microsoft Vendor
    2023-01-20T04:36:59.2266667+00:00

    Hello,
    The returned object is of type IEnumerable<Contact>, you can determine the type and declare it publicly. See IEnumerable<T> Interface (System.Collections.Generic) | Microsoft Learn

    In addition, you can convert the type if you would like to. For more details, please refer to ObservableCollection<T> Class (System.Collections.ObjectModel) | Microsoft Learn

    IEnumerable<Contact> contacts;
    ObservableCollection<Contact> ContactsCollection;// I declare two fields
    
    
    private async void OnCounterClicked(object sender, EventArgs e)
          {
            contacts  = await Microsoft.Maui.ApplicationModel.Communication.Contacts.GetAllAsync();// this one is type of IEnumerable<Contact>
           //var contacts = await Microsoft.Maui.ApplicationModel.Communication.Contacts.GetAllAsync();
           // ContactsCollection = new ObservableCollection<Contact>(contacts); //
    this one is type of ObservableCollection<Contact>
          }
    

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

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