locked
contact picker contract: which type of selection?

    Question

  • Hello again.

    Sorry for asking this here, but I can't test any of the code until my previous developer lic problem is solved (http://social.msdn.microsoft.com/Forums/en-US/toolsforwinapps/thread/6648c763-8cfb-48d3-aad8-2eb3862cc21e).

    Before getting that damn error, I was building an app which implemented the contact picker contract. in order to do that, I've added a contact picker launch page which uses a listview control to show the contacts which can be picked by the user. 

    Here's the question: how do I know if the picker has been opened to select  one or several contacts? I mean, I've looked at the ContactPickerUI but I think that it doesn't have any property which lets me get that info from it. Btw, I'd like to know this because I think I need to adapt the selection mode of the listview to the selection mode of the contact picker. Am I wrong?

    Once again, I don't have any possibility of testing this, so I'm sorry for bothering you with what might be a stupid question... 


    Luis Abreu

    Friday, May 04, 2012 8:49 AM

Answers

  • Hi Luis,

    Silly me, you are correct.  I just traced though in the debugger and saw that field and looked up the reference to it (no excuse but super busy today).

    Good news...  You do not care what the selection mode is!  The ui presents you with all the contacts and will only allow you to select Multiple if you requested multiple.  Make sense?

    All you do is provide your list of contacts back in this contract and the client UI decides if you are allowed to select one or multiple.

    -Jeff


    Jeff Sanders (MSFT)

    Friday, May 04, 2012 8:11 PM
    Moderator

All replies

  • No question is stupid my friend!

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.contacts.provider.contactpickerui.selectionmode.aspx

    When activated you can query the selection mode.

    Get the contactPickerUI from the activation arguments and then get the selection mode property:

    if (e.kind === Windows.ApplicationModel.Activation.ActivationKind.contactPicker) { contactPickerUI = e.contactPickerUI;

    var selectionMode = contactPickerUI.selectionMode

    }


    -Jeff

    Jeff Sanders (MSFT)

    Friday, May 04, 2012 2:12 PM
    Moderator
  • Hello again Jeff.

    Yes, I've noticed that property. but if you scroll a little down, you'll notice that it seems to refer to the type of object which is returned (is it returning a contact or a collection of fields?). At least, that's what I gather from its return type: http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.contacts.contactselectionmode.aspx


    Luis Abreu

    Friday, May 04, 2012 4:21 PM
  • Hi Luis,

    Silly me, you are correct.  I just traced though in the debugger and saw that field and looked up the reference to it (no excuse but super busy today).

    Good news...  You do not care what the selection mode is!  The ui presents you with all the contacts and will only allow you to select Multiple if you requested multiple.  Make sense?

    All you do is provide your list of contacts back in this contract and the client UI decides if you are allowed to select one or multiple.

    -Jeff


    Jeff Sanders (MSFT)

    Friday, May 04, 2012 8:11 PM
    Moderator
  • hello again Jeff.

    Are you talking about the contact basket? I'm asking this because I'm interested in synchronizing the basket with my listview control which will be shown when the user picks my app from the contact picker dialog options...

    As you know, I still have no dev machine, but today I've managed to get the laptop of a friend of mine and I've run my code there. Interestingly, the listview I'm using in my contact picker contract launch page will only allow me to select one item from it when I used the pickSingle method and it allowed me to select several when I opted for the pickMultiple method...Even though I've configured it to allow multiple selection, it will only allow a single selection when I use the pickSingleXXX method...

    wow...this is kind of weird...how does the it know I'm using a listview? I mean, and if I decided to use several checkboxes? would it also clear the other checkboxes?

    btw, an extra question: when selecting only one item, I've noticed that If the next selection is an item placed after the current selection, then the listview will replace the previous selected item with the new one. However, if I pick a previous item, then it won't change its selections. For instance, suppose you've got this:

    A  C  E

    B  D  F

    If you select D and then click over E, the listview will show E as selected. However, if you select C and then click over A, C remains selected. In this case, to select A I need to toggle C and only then will clicking over A select it. Is this a known issue or have I just screwed my code somewhere (btw, I'm using grouping here)


    Luis Abreu


    Friday, May 04, 2012 8:30 PM
  • Hey Luis,

    It seems there is something weird in your code.  Selecting items in a listview does not normally do this.

    -Jeff


    Jeff Sanders (MSFT)

    Monday, May 14, 2012 6:26 PM
    Moderator
  • Hello again Jeff. 

    hum...now that you mention it, I think I understand the problem: it's related with the first question I made.

    Let's go back: I asked if there was a way to know if the basket was opened in a single or multi mode. you said it's not important because the basket takes care of that for you. If I understood it correctly, you're saying that the contact basket won't let you add more than one item when you open it in single selection mode, right? And yes, I can confirm this behavior.

    Now, since I can't really know how the used opened the contacts dialog, I've set my listview to enable multi selection and the tapBehavior is set to toggleSelect. And this is When  the weird  behavior I've described happens. Suppose I've selected D and then B (from the previous post, where I tried to show the organization of the list). ok, so here's what happens when I try to select B after selecting D:

    1. I get a list of selected contacts from the listview. Now, there are two items selected and they're ordered according with the presentation shown in the list view, ie, selectedContacts = [ B, D]

    2. I check if B is in the contacts basket. It's not (remember that D was the previously selected option!), so I add B to the basket and proceed to the next element in the selectedContacts array. Unfortunately, this has a bad side effect: it will queue a removal call for D because the contacts picker dialog was opened in single selection mode

    3. I move on to the next item in selectedContacts. I check if D is in the contacts basket. Even though it was in 1.), now it's gone, so it will add D again. Once more, since the picker will only add one element to the basket, it will generate a removal call for the previous item (which is B due to step 2)

    4. the code that handles the contactremoved event is now called twice!!! One for D and another for B. Since my code needs to sync the listview with the items shown in the basket, I end up removing both of the from the selection and from the basket.

    Now, when the selection is   B, D (ie, D is selected after B)  then everything works ok:

    1. the selection array now has [B,D]

    2. checking if B is in the basket returns true, and B won't be added again

    3. D is not in the basket and it does get added. This means the contactspickerui will remove B from the basket and that will end up calling the contactremoved event (which will then remove the item from the listview selected list!)

    and that's why what I said works ok.

    Now, back to the original question: there must be some way for me to know if the contacts picker allows only single selection or multiple selection. Now, without having a property or a method, I think that the only option I have is to add 2 items to the basket and then check if both of them are in the basket. this seems to be an awful lot of trouble to solve my problem, doesn't it??

    Hope I've explained myself...if not, please let me know because I think we really need a way to know if the contact dialog was opened in single or multiple selection mode!

    thanks.


    Luis Abreu

    Monday, May 14, 2012 8:15 PM
  • Jeff, the workaround works...I mean, kind of.

    Here's the ugly code I had to resort to. this is the method that performs the checks:

    function verificaSePermiteVarios() {
        var aux = [
            {
                id: contactos[0],
                ct: criaContactoParaBasket(contactos[0])
            },
            {
                id: contactos[1],
                ct: criaContactoParaBasket(contactos[1])
            }
        ];
        contactPickerUI.addContact(aux[0].id, aux[0].ct);
        contactPickerUI.addContact(aux[1].id, aux[1].ct);
        setTimeout(function () {
            var permiteVarios = contactPickerUI.containsContact(aux[0].id) &&
            contactPickerUI.containsContact(aux[1].id);
           //update the list!
            lista.selectionMode = permiteVarios ? "multi" : "single";
           //remove items
           contactPickerUI.removeContact(aux[0].id);

            contactPickerUI.removeContact(aux[1].id);
            verificouSelecao = true;;
        },
        500);
    }

    Btw, it can't be called from the activation handler or you'll end up with an exception (access denied while trying to add items to the contact's basket!). That means I had to call it from the selection changed event. not really good. And when you have multiple selection enabled, you even see it get shown and hidden quickly...

    Come on guys, we need a property for knowing if the contact picker UI allows single or multiple selection!!! ;)


    Luis Abreu

    Monday, May 14, 2012 8:45 PM