0

Although this has been posted before on StackOverflow but i think none of those reflect my issue and none of those solutions work for me either. So i'm developing a Windows Phone app and my workflow is a bit like this:

  • App starts
  • ContactPicker opens up
  • User selects one or multiple contacts
  • Based on how many contacts he selected, that many PivotItems are added into the Pivot.

My code is as follows:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // TODO: Prepare page for display here.

        // TODO: If your application contains multiple pages, ensure that you are
        // handling the hardware Back button by registering for the
        // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
        // If you are using the NavigationHelper provided by some templates,
        // this event is handled for you.

        SelectContacts();
    }

    private async Task SelectContacts()
    {
        var picker = new ContactPicker();
        picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);

        ContactsList = (List<Contact>)await picker.PickContactsAsync();
        DisplayContacts();
    }

    private void DisplayContacts()
    {
        if (ContactsList != null)
        {
            foreach (var item in ContactsList)
            {
                PivotItem pivotItem = new PivotItem();
                pivotItem.Header = item.FirstName.ToString();

                ContentRoot.Items.Add(pivotItem);
            }
        }
    }

According to me, in SelectContacts() method, the app should wait at the await call and once it gets back the list of contacts, than it should execute the DisplayContacts() method but its not working. I've tried multiple other variations of this code and they aren't working either.

Haseeb Ahmed
  • 643
  • 5
  • 16

2 Answers2

1

await the SelectContacts() method and add the DisplayContacts() method beneath it. Remove the DisplayContacts() method from SelectContacts()

await SelectContacts();
DisplayContacts();
Tim Southard
  • 604
  • 4
  • 16
  • Tried that as well. Doesn't work. I can still see the ContactPicker running on my emulator while DisplayContacts() doesn't wait for SelectContacts() and starts executing. – Haseeb Ahmed Feb 07 '15 at 05:06
  • Try not casting the await as List. Instead, do something like 'var contacts = await picker.getContactsAsync(); if (contacts != null) ContactsList = (List)contacts;' – Tim Southard Feb 07 '15 at 05:15
0

I don't know the complete reason why but i figured it out that since i was making the PickContactsAsync() call in the OnNavigatedTo() event, that is why it wasn't working as expected. Once i moved the PickContactsAsync() call into the PageLoaded() event handler, it started working as usual.

Haseeb Ahmed
  • 643
  • 5
  • 16