2012-07-03 51 views
2

我目前在Windows8應用程序中實現自定義數據源。但是,我遇到了一些問題:沒有顯示數據。用WinJS自定義數據源?

首先,這裏是代碼:

var dataArray = [ 
    { title: "Basic banana", text: "Low-fat frozen yogurt", picture: "images/60banana.png" }, 
    // Other data taken from Windows8 ListView quick start 
    { title: "Succulent strawberry", text: "Sorbet", picture: "images/60strawberry.png" } 
]; 

var searchAdDataAdapter = WinJS.Class.define(

    function() {}, // Constructor 

    { 
     itemsFromIndex: function (requestIndex, countBefore, countAfter) { 

      var that = this; 

      if (requestIndex >= that._maxCount) { 
       return WinJS.Promise.wrapError(new WinJS.ErrorFromName(UI.FetchError.doesNotExist)); 
      } 

      var fetchSize, fetchIndex; 

      // See which side of the requestIndex is the overlap. 
      if (countBefore > countAfter) { 

       // Limit the overlap 
       countAfter = Math.min(countAfter, 10); 

       // Bound the request size based on the minimum and maximum sizes. 
       var fetchBefore = Math.max(
        Math.min(countBefore, that._maxPageSize - (countAfter + 1)), 
        that._minPageSize - (countAfter + 1) 
       ); 

       fetchSize = fetchBefore + countAfter + 1; 
       fetchIndex = requestIndex - fetchBefore; 

      } else { 
       countBefore = Math.min(countBefore, 10); 
       var fetchAfter = Math.max(Math.min(countAfter, that._maxPageSize - (countBefore + 1)), that._minPageSize - (countBefore + 1)); 
       fetchSize = countBefore + fetchAfter + 1; 
       fetchIndex = requestIndex - countBefore; 
      } 

      // Create an array of IItem objects: 
      // results =[{ key: key1, data : { field1: value, field2: value, ... }}, { key: key2, data : {...}}, ...]; 
      for (var i = 0, itemsLength = dataArray.length ; i < itemsLength ; i++) { 

       var dataItem = dataArray[i]; 
       results.push({ 
        key: (fetchIndex + i).toString(), 
        data: dataArray[i] 
       }); 
      } 

      // Get the count. 
      count = dataArray.length; 

      return { 
       items: results, // The array of items. 
       offset: requestIndex - fetchIndex, // The index of the requested item in the items array. 
       totalCount: count 
      }; 

     }, 

     getCount: function() { 
      return dataArray.length; 
     } 
    } 
); 

var searchAdDataSource = WinJS.Class.derive(WinJS.UI.VirtualizedDataSource, function() { 
    this._baseDataSourceConstructor(new searchAdDataAdapter()); 
}); 

// Create a namespace to make the data publicly 
// accessible. 
var publicMembers = { 
    itemList: new searchAdDataSource() 
}; 

WinJS.Namespace.define("DataExample", publicMembers); 

我知道代碼是有點長,但它的主要部分是來自微軟官方的自定義數據源快速啓動拍攝。

我試圖調試它,但似乎代碼中包含的itemFromIndex從來沒有使用過(我的斷點永遠不會到達)。

的HTML代碼是:

<div id="basicListView" data-win-control="WinJS.UI.ListView" 
    data-win-options="{itemDataSource : DataExample.itemList.dataSource}"> 
</div> 

我不使用任何模板的時刻,以簡化代碼爲更多的,我可以。數據通常以這種方式顯示在文本中(但不會顯示)。

有沒有這個偉大的社區任何想法?

此外,我不明白countbeforecountd後參數,即使與文檔。有人可以用其他詞語向我解釋嗎?

非常感謝! :)

回答

0

嘗試修改你的HTML代碼如下:

<div id="basicListView" data-win-control="WinJS.UI.ListView" 
    data-win-options="{itemDataSource : DataExample.itemList}"> 
</div> 

無需調用.datasource成員,你直接談話的數據源。

+0

您好Xavier,我已經實現了自定義數據源的ListView控件。我能夠在第一頁顯示數據。但是當我滾動列表視圖控制到最後一個項目它不會加載更多pages.I不知道在哪裏我錯了,你能幫我嗎? –