Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContactPicker is not working in Windows Phone 8.1 Silverlight

I've tried to get contact info in Windows Phone 8.1 SL app by following Quickstart: Selecting user contacts

In my function,

    private async void PickAContactButton_Click(object sender, RoutedEventArgs e)
    {
        var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
        contactPicker.desiredFieldsWithContactFieldType.add(Windows.ApplicationModel.Contacts.ContactFieldType.email);
        Contact contact = await contactPicker.PickContactAsync(); // this throws System.NotImplementedException
        // Additional information: The method or operation is not implemented.

        if (contact != null)
        { ... }
     }

Exact same function works in Windows Phone 8.1 RT. ContactPicker class is supported in both WP 8.1 RT and WP 8.1 SL according to this reference.

Any idea what is going on ?

like image 859
ToMonika Avatar asked Nov 11 '22 00:11

ToMonika


1 Answers

I had this behaviour today in my Universal Store App for Win 8.1, so may be this helps you out. I had different exceptions though (FileNotFoundException and just plain System.Exception), so I'm not really certain this is the same issue.

As far as my experiments go, this is what is currently needed to make ContactPicker work:

  • ContactPicker instance must be created in the UI thread
  • contactPicker.DesiredFieldsWithContactFieldType must have exactly one item (0 or >1 items yield exception)

This is what I ended up doing:

// using Windows.ApplicationModel.Core;

// in an async method:
Contact user = null;
AutoResetEvent resetEvent = new AutoResetEvent(false);
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
    CoreDispatcherPriority.Normal, 
    (async ()=>{
      ContactPicker contactPicker = new ContactPicker();
      contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);
      user = await contactPicker.PickContactAsync();
      resetEvent.Set();
    }
);
resetEvent.WaitOne();
if (user != null) {
    // do smth
}
like image 57
Michael Antipin Avatar answered Nov 14 '22 22:11

Michael Antipin