【问题标题】:reference listview windows 8 itemtemplate dynamically javascript动态引用listview windows 8 itemtemplate javascript
【发布时间】:2012-10-23 15:15:19
【问题描述】:

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

有可能吗?如何实现?

【问题讨论】:

    标签: windows-8 microsoft-metro winjs


    【解决方案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;
           });
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      相关资源
      最近更新 更多