2

以下代码构造函数加载了一个 Qml 文件,该文件从远程 Json 数据源加载数据,这工作得很好。当我调用 startMenu 方法时,我在 MainMenu.Qml 中使用完全相同的 Qml,但是一旦我调用 dataSource.load(),我的应用程序就会在 QDeclarativeExpression::hasError() 处崩溃,之后它就会挂起。没有发出错误消息或异常。

如果我移动 dataSource.load(); 一个按钮单击它可以工作,但是当页面加载时我无法加载我的数据。

#include "MyAppBB.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Button>
#include <bb/data/DataSource>
#include <wifi/wifi_service.h>

using namespace bb::cascades;

MyAppBB::MyAppBB(bb::cascades::Application *app) :
        QObject(app) {
    // create scene document from main.qml asset
    // set parent to created document to ensure it exists for the whole application lifetime
    bb::data::DataSource::registerQmlTypes();
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    this->myApp = app;

    QDeclarativePropertyMap* propertyMap = new QDeclarativePropertyMap;
    propertyMap->insert("name", QVariant(QString("Wes Barichak")));
    propertyMap->insert("phone", QVariant(QString("519-555-0199")));

    // create root object for the UI
    this->root = qml->createRootObject<AbstractPane>();

    qml->setContextProperty("propertyMap", propertyMap);
    qml->setContextProperty("root", this);

    QObject *newButton = root->findChild<QObject*>("btnLogin");
    QObject::connect(newButton, SIGNAL(clicked()), this, SLOT(loginClick()));
    // set created root object as a scene
    app->setScene(root);
}

void MyAppBB::startMenu() {
    QmlDocument *qml2 = QmlDocument::create("asset:///MainMenu.qml").parent(this);
    AbstractPane *root2 = qml2->createRootObject<AbstractPane>();

    this->myApp->setScene(root2);
}

这是Qml

import bb.cascades 1.0
import bb.data 1.0

Page {
    Container {
        layout: StackLayout {
        }


        TextField {
            id: txtUsername
        }

        ListView {
            id: myListView

            // Associate the list view with the data model that's defined in the
            // attachedObjects list
            dataModel: dataModel
            listItemComponents: [
                ListItemComponent {
                    type: "item"

                    // Use a standard list item to display the data in the data
                    // model
                    StandardListItem {

                        title: ListItemData.identificationType
                        description: ListItemData.identificationTypeId
                    }
                }
            ]
        }
        attachedObjects: [
            GroupDataModel {
                id: dataModel

                // Sort the data in the data model by the "pubDate" field, in
                // descending order, without any automatic grouping
                sortingKeys: [
                    "ID"
                ]
                sortedAscending: false
                grouping: ItemGrouping.None
            },
            DataSource {
                id: dataSource

                source: "http://192.168.150.1/JsonMobileApiService/Service1.svc/GetIDTypes"

                type: DataSourceType.Json
                onDataLoaded: {
                    dataModel.clear();
                    dataModel.insertList(data);
                }
            }
        ]
        onCreationCompleted: {
            txtUsername.setText("boo!");
            // Statement below causes crash
            dataSource.load();
        }
    }
}
4

1 回答 1

2

里面有一个onCreationCompletedAbstractPane,你可以用它来调用load。

以 API 规范XMLDataAccess为例。我正在使用它自己从 XML 文件中加载数据,它可以工作

  import bb.cascades 1.0
  import bb.data 1.0
  Page {
  content: ListView {
    id: listView
    dataModel: dataModel
    ...
  }
  attachedObjects: [
    GroupDataModel {
      id: dataModel
    },
    DataSource {
      id: dataSource
      source: "contacts.xml"
      query: "/contacts/contact"
      onDataLoaded: {
        dataModel.insertList(data);
      }
    }
  ]
  onCreationCompleted: { dataSource.load(); }
}
于 2013-01-12T22:01:53.263 回答