我正在创建一个 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;
});
}