I'm trying to create a small WP 8.1 application that displays a ContactPicker as it starts. I'm using Visual Studio Community 2015 on Windows 10 Preview build 10532 to create the application. So, as soon as I create a Windows Phone Blank App, I add a button and this code.
public PivotPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
this.pickContactCommandButton.Click += PickContactCommandButton_Click;
}
private async void PickContactCommandButton_Click(object sender, RoutedEventArgs e)
{
try
{
ContactPicker contactPicker = new ContactPicker();
IList<Contact> pickedContacts = await contactPicker.PickContactsAsync();
int pickedCount = pickedContacts != null ? pickedContacts.Count : 0;
if (pickedCount > 0)
{
}
}
catch (Exception ex)
{
MessageDialog md = new MessageDialog(ex.Message);
await md.ShowAsync();
}
}
Then I deploy the app to my Windows Phone 8.1 phone with Denim version installed, and registered for development (https://msdn.microsoft.com/en-us/library/windows/apps/ff769508%28v=vs.105%29.aspx?f=255&MSPPError=-2147217396).
When I run the app on the phone and I press the button I get the message in the title and the contact picker doesn't show up. The problematic line is the one with await. It works well if I deploy the same app to my Windows 10 Mobile phone.
I have this set in the Package.appxmanifest file:
<Capabilities>
<m3:Capability Name="contacts" />
</Capabilities>
What could be the problem?
The reason why the contact picker didn't show and the exception was thrown is that this line of code was missing before calling the contact picker pick method:
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);
So, basically you need to add at least one ContactFieldType on Windows Phone 8.1, otherwise contact picker will crash...
Problem solved.
User contributions licensed under CC BY-SA 3.0