locked
Cant get onItemInvoke event

    Question

  • Hope someone will help me:

    I need to get click event on my ListView element. I read about ListView.oniteminvoked event, and used it:

                <div id="groupedListView"
                    data-win-control="WinJS.UI.ListView" 
                    data-win-options="{itemDataSource: myData.groupedItemsList.dataSource, 
                        itemTemplate: select('#mediumListIconTextTemplate'),
                        groupDataSource: myData.groupedItemsList.groups.dataSource,
                        groupHeaderTemplate: select('#headerTemplate'),
                        layout: {type: WinJS.UI.GridLayout},
                        oniteminvoked : handler}">
                </div>

    JavaScript:

        // Create the groups for the ListView from the item data and the grouping functions
        var groupedItemsList = itemsList.createGrouped(getGroupKey, getGroupData, compareGroups);
    
        WinJS.Namespace.define("myData",
              {
                  groupedItemsList: groupedItemsList
              });
    
        function handler(eventInfo) {
            var dlg = new Windows.UI.Popups.MessageDialog("Ta-da!", "Ta-da!");
            dlg.showAsync();
        }
        groupedItemsList.addEventListener("iteminvoked", handler, false);
    But when i clicking on listView nothing happening -( 

    Friday, October 26, 2012 9:48 PM

Answers

  • Hi,

    Please make sure you are adding the handler in the main js, the code as follow:

    (function () {
        "use strict";
    
        var app = WinJS.Application;
        var activation = Windows.ApplicationModel.Activation;
        WinJS.strictProcessing();
    
        app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize
                    // your application here.
                } else {
                    // TODO: This application has been reactivated from suspension.
                    // Restore application state here.
                }
                args.setPromise(WinJS.UI.processAll().then(function completed() {                
                    // addEventListener
                    var listView = document.getElementById("basicListView");
                    listView.addEventListener("iteminvoked", handler);
    
                }));
            }
        };
    
        app.oncheckpoint = function (args) {
            // TODO: This application is about to be suspended. Save any state
            // that needs to persist across suspensions here. You might use the
            // WinJS.Application.sessionState object, which is automatically
            // saved and restored across suspension. If you need to complete an
            // asynchronous operation before your application is suspended, call
            // args.setPromise().
        };
    
        function handler(eventInfo) {
            var dlg = new Windows.UI.Popups.MessageDialog("Ta-da!", "Ta-da!");
            dlg.showAsync();
    
        }
    
        app.start();
    })();


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    • Marked as answer by Serj Nights Saturday, October 27, 2012 8:10 PM
    Saturday, October 27, 2012 8:04 AM

All replies

  • Hi,

    Please make sure you are adding the handler in the main js, the code as follow:

    (function () {
        "use strict";
    
        var app = WinJS.Application;
        var activation = Windows.ApplicationModel.Activation;
        WinJS.strictProcessing();
    
        app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize
                    // your application here.
                } else {
                    // TODO: This application has been reactivated from suspension.
                    // Restore application state here.
                }
                args.setPromise(WinJS.UI.processAll().then(function completed() {                
                    // addEventListener
                    var listView = document.getElementById("basicListView");
                    listView.addEventListener("iteminvoked", handler);
    
                }));
            }
        };
    
        app.oncheckpoint = function (args) {
            // TODO: This application is about to be suspended. Save any state
            // that needs to persist across suspensions here. You might use the
            // WinJS.Application.sessionState object, which is automatically
            // saved and restored across suspension. If you need to complete an
            // asynchronous operation before your application is suspended, call
            // args.setPromise().
        };
    
        function handler(eventInfo) {
            var dlg = new Windows.UI.Popups.MessageDialog("Ta-da!", "Ta-da!");
            dlg.showAsync();
    
        }
    
        app.start();
    })();


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    • Marked as answer by Serj Nights Saturday, October 27, 2012 8:10 PM
    Saturday, October 27, 2012 8:04 AM
  • Ty, i fixed my problem!)

    My mistake was that i added event listener in wrong place (and in wrong js file). 

    In fact, to solve the problem in my case, i used this code not in default.js, but in the js file of the page where listView element was placed. (when i tried to use default.js file it didn't worked)

    Sorry for my english, I wrote it because I think that it can be useful for someone)

    Saturday, October 27, 2012 8:17 PM