1

我正在用 HTML5/Javascript 开发一个 win8 应用程序;我需要在外部 html 文件中设置 itemtemplate 并在需要时引用它。

有可能吗?怎么做?

4

1 回答 1

1

我认为您不能从外部 html 文件加载它。但是,您可以完全在 JavaScript 中创建模板。

以下代码从此处复制:http: //msdn.microsoft.com/en-us/library/windows/apps/hh700705.aspx

    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 () {
                var lView = document.getElementById("templateFunctionListView").winControl;
                lView.itemTemplate = itemTemplateFunction;

            }));

        }
    };

   function itemTemplateFunction(itemPromise) {

       return itemPromise.then(function (item) {
           var div = document.createElement("div");

           var img = document.createElement("img");
           img.src = item.data.picture;
           img.alt = item.data.title;
           div.appendChild(img);

           var childDiv = document.createElement("div");

           var title = document.createElement("h4");
           title.innerText = item.data.title;
           childDiv.appendChild(title);

           var desc = document.createElement("h6");
           desc.innerText = item.data.text;
           childDiv.appendChild(desc);

           div.appendChild(childDiv);

           return div;
       });
    };
于 2012-11-06T13:46:50.680 回答