2

現在、Windows8 アプリケーションにカスタム データ ソースを実装しています。ただし、データが表示されないという問題が発生しました。

まず、コードは次のとおりです。

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);

コードが少し長いことは承知していますが、その大部分は、公式の Microsoft カスタム データ ソース クイック スタートから取得したものです。

デバッグしようとしましたが、itemFromIndexに含まれるコードが使用されていないようです (ブレークポイントに到達しません)。

HTML コードは次のとおりです。

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

可能な限りコードを単純化するために、現時点ではテンプレートを使用しません。通常、データはこのようにテキストで表示されます (ただし、何も表示されません)。

この素晴らしいコミュニティの 1 つにアイデアはありますか?

さらに、ドキュメントがあっても、 countBeforeおよびcountAfterパラメーターがわかりません。誰か他の言葉で説明してくれませんか?

どうもありがとう!:)

4

1 に答える 1

0

HTML コードを次のように変更してみてください。

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

データソースと直接やり取りしているため、.datasource メンバーを呼び出す必要はありません。

于 2012-08-13T19:50:54.280 に答える