2012-11-05 79 views
2

以下代碼構造函數加載了一個Qml文件,該文件從遠程Json數據源加載數據,並且這種工作非常好。當我調用startMenu方法時,我在MainMenu.Qml中使用完全相同的Qml,但只要我調用dataSource.load(),我的應用程序在QDeclarativeExpression :: hasError()處崩潰,之後它就掛起。沒有錯誤消息或發出異常。BlackBerry 10 QML應用程序在數據源上崩潰加載

如果我移動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(); 
     } 
    } 
} 

回答

2

有一個在AbstractPaneonCreationCompleted插槽,你可以用它來打電話負載。

它以XMLDataAccess的API規範爲例。我使用它來從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(); } 
}