locked
PopupMenu sample: is this a bug?

    Question

  • Hello guys.

    The contextmenusample project (windows 8 samples) has the following code for opening a context menu:

    var menu = new Windows.UI.Popups.PopupMenu();
    menu.commands.append(new Windows.UI.Popups.UICommand("Copy", null, 1));
    menu.commands.append(new Windows.UI.Popups.UICommandSeparator);
    menu.commands.append(new Windows.UI.Popups.UICommand("Highlight", null, 2));
    menu.commands.append(new Windows.UI.Popups.UICommand("Look up", null, 3));
    menu.showForSelectionAsync(getSelectionRect()).then(function (invokedCommand) {
        if (invokedCommand != null) {
            switch (invokedCommand.id) {
                case 1: // Copy
                    var selectedText = window.getSelection();
                    copyTextToClipboard(selectedText);
                    sdkSample.displayStatus("'Copy' button clicked and '" + selectedText + "' copied to clipboard");
                    break;
                case 2: // Highlight
                    // Add command handler code here
                    sdkSample.displayStatus("'Highlight' button clicked");
                    break;
                case 3: // Look up
                    // Add command handler code here
                    sdkSample.displayStatus("'Look up' button clicked");
                    break;
                default:
                    break;
            }
        } else {
            // The command is null if no command was invoked.
            sdkSample.displayStatus("Context menu dismissed");
        }
    }
    );

    Yes, it does work and the context menu is shown. Why doesn't it work if I remove the then call?

    var menu = new Windows.UI.Popups.PopupMenu();
    menu.commands.append(new Windows.UI.Popups.UICommand("Copy", null, 1));
    menu.commands.append(new Windows.UI.Popups.UICommandSeparator);
    menu.commands.append(new Windows.UI.Popups.UICommand("Highlight", null, 2));
    menu.commands.append(new Windows.UI.Popups.UICommand("Look up", null, 3));
    menu.showForSelectionAsync(getSelectionRect());

    Yes, I won't be notified and won't be able to know which command was chosen, but shouldn't I at least see the menu???

    thanks.


    Luis Abreu

    Thursday, February 09, 2012 12:02 PM

Answers

All replies

  • What happens if you supply an empty function in your code: .then(function(){}); ?

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, February 09, 2012 12:49 PM
    Moderator
  • If I'm not mistaken, if you supply it it, it will work. BUt the current behavior must surelly be a bug, no?

    I mean, if I pass handlers to the commands, then probably I just waht to show the menu and have no need for being called when the menu is dismissed (since the command that is responsible for dismissing the menu has its own callback, I should be able to know what happened, right?)...


    Luis Abreu

    Sunday, February 12, 2012 2:57 PM
  • Please check it with empty function!
    Monday, February 13, 2012 4:59 AM
  • Hi Luis,

    Yes this is a known bug.  The menu will come down when the object goes out of scope.  You can either keep a reference around to the menu or use the .then() patterne (which does the same thing).

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, February 15, 2012 1:28 PM
    Moderator