0

I'm trying to allow the user to select a contact from the People app this way:

private async Task<System.Collections.Generic.KeyValuePair<string, string>> SelectAContactForASlot()
{
    KeyValuePair<string, string> kvp; // = new KeyValuePair<string, string>();
    var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
    contactPicker.CommitButtonText = "Select";
    var contact = await contactPicker.PickSingleContactAsync();
    if (contact != null)
    {
        kvp = new KeyValuePair<string, string>(contact.Name, contact.Emails[0].ToString());
        return kvp;
    }
    return kvp = new KeyValuePair<string, string>("No Name found", "No email found");
}

The People app does get invoked, but it looks like this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ People v

Something went wrong, and this app can't pick contacts right now.

Try selecting the app again.

            | Select |  | Cancel |

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I added a couple of contacts yesterday, so it does contain contacts. Is there something wrong with my code, or how else can I solve this problem?

Damir Arh
  • 17,637
  • 2
  • 45
  • 83
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Have you added the `ID_CAP_CONTACTS` capability to your manifest file (`WMAppManifest.xml`)? – keyboardP Nov 19 '12 at 01:14
  • The manifest file is named Package.appxmanifest (the same for all Windows Store apps); the Declarations portion of Package.appxmanifest has a "Contact Picker" item, but I'm not at all sure this is required for simply invoking the native People app; its description is "Registers the app as a people picker, making contact details in the app available to other Windows 8 apps..." – B. Clay Shannon-B. Crow Raven Nov 19 '12 at 05:25
  • Sorry, I misread the tag as Windows Phone 8. Have you tried enabling it just to see if it works? – keyboardP Nov 19 '12 at 12:57
  • If I add the "Contact Picker" declaration, it also wants to know Executable, entry point, and Start page (I don't know what I should put there; I really don't think this applies to my scenario). – B. Clay Shannon-B. Crow Raven Nov 19 '12 at 15:56

1 Answers1

1

I tried your code and it opened the contact picker as expected. Just for a test, try creating a new application with a single button which calls your method from its Click event handler, like I did.

Also, you might want to freshly logon / reboot to your machine if the problem persists. I know I had similar problems with the sharing functionality in the past - after handling it wrong in my code, even correct code didn't work any more until a reboot.

While I'm at it: you might want to change your code a little bit - to actually get the email address, replace contact.Emails[0].ToString() with contact.Emails[0].Value:

private async Task<System.Collections.Generic.KeyValuePair<string, string>> SelectAContactForASlot()
{
    KeyValuePair<string, string> kvp; // = new KeyValuePair<string, string>();
    var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
    contactPicker.CommitButtonText = "Select";
    var contact = await contactPicker.PickSingleContactAsync();
    if (contact != null)
    {
        kvp = new KeyValuePair<string, string>(contact.Name, contact.Emails[0].Value);
        return kvp;
    }
    return kvp = new KeyValuePair<string, string>("No Name found", "No email found");
}

Don't forget to get handle the case when the contact doesn't have any email addresses, as well.

Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • Thanks; I changed my code to use Value instead of ToString(), and added a check for a null Value (email address). However, I can get the Contact Picker to display now (apparently the temporary glitch was fixed by a reboot or something), but there are still no Contacts in it, so it's still a problem. I added what contacts I could (from att.net, etc.) and even added a couple manually, and it still shows no contacts available. – B. Clay Shannon-B. Crow Raven Nov 20 '12 at 15:39
  • @Clay It just randomly switches between showing the contacts and not showing them for you? You do always see the contacts if you open the People app, though? Do you by any chance have multiple contact pickers registered and for some reason it's not always the same one selected? You can switch between them by tapping the title (it should be People) of the picker in the top left of the screen. – Damir Arh Nov 22 '12 at 05:27
  • The People app always displays, and I can select "People" from the dropdown (the only thing there); it's at that point, that it usually says it's not available - I've only seen the list of contacts once - just enough to know that my code works, something else is the problem. – B. Clay Shannon-B. Crow Raven Nov 22 '12 at 15:11
  • @ClayShannon Are you by any chance trying to implement a Contact Picker yourself either in this app or another one? This could cause problems if it's not (yet) implemented properly. Does it work if you try running your app and opening the contacts picker immediately after rebooting? Do you have a possibility to try it out on a different machine. – Damir Arh Nov 22 '12 at 20:06