3

我根据我在 MSDN 中阅读的链接使用以下代码。

 public async void getFavouriteList()
    {
        try
        {
            ContactStore store = await ContactStore.CreateOrOpenAsync();


            var queryOptions = new ContactQueryOptions();
            queryOptions.DesiredFields.Add(KnownContactProperties.DisplayName);

            ContactQueryResult result = store.CreateContactQuery(queryOptions);

            var contactCount = await result.GetContactCountAsync();
            var contacts = await result.GetContactsAsync(0, contactCount);

            //IReadOnlyList<StoredContact> contacts = await result.GetContactsAsync();

            foreach (StoredContact item in contacts)
            {
                // I am not getting any contacts so this debug line is not printed !!!
                Debug.WriteLine("this is not printed");

                Debug.WriteLine(item.DisplayName + ":" + item.RemoteId);
            }
        }
        catch(Exception e)
        {
            Debug.WriteLine("this is not printed 7");
            Debug.WriteLine(e.Message);
        }
    }

请在我的代码中指出问题。

注意事项 我使用的是 Windows 8.1 通用应用程序模板。我还在我的应用程序功能中添加了联系人和约会。

每件事对我来说都很完美..如果我遗漏了什么,请帮助我!

4

2 回答 2

1

是的,你当然可以做到。

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var picker = new Windows.ApplicationModel.Contacts.ContactPicker();
    picker.DesiredFieldsWithContactFieldType.Add(Windows.ApplicationModel.Contacts.ContactFieldType.Email);
    var contact = await picker.PickContactAsync();

    if (contact == null)
        return;

#if WINDOWS_APP
    var button = sender as Button;
    var visual = button.TransformToVisual(null);
    var point = visual.TransformPoint(new Windows.Foundation.Point());
    var rect = new Windows.Foundation.Rect(point, button.RenderSize);
    Windows.ApplicationModel.Contacts.ContactManager.ShowContactCard(contact, rect);
#elif WINDOWS_PHONE_APP
    var dialog = new Windows.UI.Popups.MessageDialog(contact.DisplayName);
    await dialog.ShowAsync();
#endif
}

祝你好运!

于 2014-06-05T17:39:04.100 回答
1

我认为您编写的代码只是拉取特定于您的应用程序的联系人。

如果您尝试获取设备上的所有联系人,您可能需要执行以下操作:

using Windows.ApplicationModel.Contacts;

var contactStore = await ContactManager.RequestStoreAsync();
var contacts = await contactStore.FindContactsAsync();

foreach (var contact in contacts) 
{
    // do whatever you want here
}
于 2014-06-25T06:45:02.013 回答