WinJS如何在页面加载时选择列表视图项

时间:2013-12-30 22:28:11

标签: windows-8 winjs

我的导航页面加载后,我正在尝试选择listview控件中的第一项。以下代码似乎没有效果。如果我将“selection.set(0)”代码放在按钮单击处理程序中,它将正常工作,但是只要页面加载这是我想要的效果就不会起作用。有人有主意吗?我必须在这里遗漏一些非常基本的东西!

HTML

<div id="basicListView"
 data-win-control="WinJS.UI.ListView"
 data-win-options="{itemDataSource : ex.itemList.dataSource,
 itemTemplate: select('#mediumListIconTextTemplate'),
 layout : {type: WinJS.UI.ListLayout},
 selectionMode: 'single',
 tapBehavior: 'directSelect'
     }">
</div>

JS

    (function () {
        "use strict";
        var _lv = null;

        WinJS.UI.Pages.define("/pages/page2/page2.html", {
            // This function is called whenever a user navigates to this page. It
            // populates the page elements with the app's data.
            ready: function (element, options) {
                // TODO: Initialize the page here.
                _lv = document.getElementById("basicListView").winControl;

                // This should select the first item in the listview though it seems to have no effect.
                _lv.selection.set(0);
            },
        });
    })();

1 个答案:

答案 0 :(得分:0)

我认为您的问题是当您尝试访问该列表时未填充该列表。 尝试将此代码放入准备好的事件中,以便您知道列表何时准备就绪:

ready: function (element, options) {

var listView = element.querySelector(".itemslist").winControl;
listView .onloadingstatechanged = function (args) {
    if (args.srcElement === listView .element && listView .loadingState === "complete") {

       //and here you set your item
       _lv = document.getElementById("basicListView").winControl;
       _lv.selection.set(0);

    }
};
}