0

我正在创建一个 Windows Metro 应用程序,其中 ListViews 作为每个页面上的主要元素。我遇到了在页面之间导航的问题,特别是导航太快:单击一个页面上的 ListView 项目以转到另一个页面,然后单击新页面。当我这样做太快时,我得到这个:

Exception was thrown but not handled in user code at line 20, column 13 in ms-appx://01c489fc-0e20-415d-ad4b-2895b4bc6e90/pages/groupedItems/groupedItems.js

0x800a138f - JavaScript runtime error: Unable to get property 'cloneNode' of undefined or null reference

If there is a handler for this exception, the program may be safely continued.

这是什么意思?是否有任何技术可以阻止这种异常并允许快速导航?

这是错误的代码:

function multisizeItemTemplateRenderer(itemPromise)
{
    return itemPromise.then(function (currentItem)
    {
        var content;

    // Grab the default item template used on the groupeditems page.
    content = document.getElementById('multiTemplate');

    /*************************
     This line is where it fails:
     *************************/
    var result = content.cloneNode(true);

    // Change the CSS class of the item depending on the group, then set the size in CSS.
    switch (currentItem.groupKey)
    {
        case "1":
            {
                // Decides which item to resize based on items index
                if (currentItem.index == 0 || currentItem.index == 1)
                {
                    result.className = "largeitemtemplate"
                }

                else
                {
                    result.className = "mediumitemtemplate"
                }
                break;
            }

        default:
            {
                result.className = "smallitemtemplate"
            }
    }
    // Because we used a WinJS template, we need to strip off some attributes 
    // for it to render.
    result.attributes.removeNamedItem("data-win-control");
    result.attributes.removeNamedItem("style");
    result.style.overflow = "hidden";

    /************************
     If this try catch isn't here, a RuntimeException occurs during quick navigation.
     ************************/
    try{
        result.getElementsByClassName("item-image")[0].src = currentItem.data.backgroundImage;
        result.getElementsByClassName("item-title")[0].textContent = currentItem.data.title;
    } catch (exception) {
        console.log(exception.name +  ": " + exception.message);
    }
    return result;
});
}
4

2 回答 2

0

我猜你的 ListView 中有很多元素。并且不等待整个页面将完全呈现。在单击此视图中的任何内容之前,请尝试再等一会儿。错误将消失 :) 单击后,新的渲染过程开始并且对象“文档”更改为新的。当前渲染器访问这个新文档,该文档具有另一个结构并且不包含 id='multiTemplate' 的元素。所以 document.getElementById('multiTemplate') 返回 null。

于 2012-07-25T13:44:39.070 回答
0

也许这个 MSDN 教程会有所帮助:

// Because we used a WinJS template, we need to strip off some attributes 
// for it to render.
result.attributes.removeNamedItem("data-win-control");
result.attributes.removeNamedItem("style");
result.style.overflow = "hidden";

// Because we're doing the rendering, we need to put the data into the item.
// We can't use databinding.
return result;
于 2013-05-06T02:18:09.243 回答