2016-03-25 6 views
0

Wir schreiben, um zu fragen, wie man eine Methode des nativen Titanium Appelator-Moduls korrekt aufruft. Unsere Entwicklungsumgebung ist wie folgt:So rufen Sie eine Methode auf, die dem nativen Windows-Modul der Appelerator Platform hinzugefügt wurde

Titanium SDK: 5.2.0
Appcelerator CLI: 5.2.0
TabletPC: Windows10pro

Unser Modul ist mit den folgenden Verfahren entwickelt worden, ;

(1) Wir haben ein Modul erstellt, das sich auf das Dokument https://github.com/appcelerator/titanium_mobile_windows#module-development bezieht Eine zusätzliche Methode aaa() wurde dem Modul hinzugefügt.

cd MY_WORKSPACE 
ti create -p windows -t module //create a module project 
cd MY_MODULE_NAME/windows 
ti build -p windows -T ws-local //build my module project 

(2) Anwendung A Fenster wurde geschaffen, um die oben Modul aufzurufen, und es gab keine Fehler während wir das Modul von der Anwendung aufrufen. Während wir jedoch die Methode aaa() aufrufen, trat eine Titanium-Fehlermeldung wie folgt auf; "aaa() Methode existiert nicht".

In der Titanium-Windows-Plattform für Windows10 wurde die DLL für das Modul korrekt erstellt. Dann fragen wir uns nur, welche Teile unserer Quelle nicht korrekt sind;

(1) Die Definition einer Methode aaa() ist nicht korrekt. (2) Der Quellcode zum Aufrufen des nativen Moduls ist nicht korrekt. (3) Der Quellcode zum Aufrufen der aaa() -Methode ist nicht korrekt.

Wir haben unseren Quellcode wie folgt beigefügt und danken Ihnen im Voraus für Ihren Kommentar und Rat.

Der Code ist wie folgt.

Fenster/src/JpNativeModuleExample.cpp

#include "JpNativeModuleExample.hpp" 
#include "Titanium/detail/TiImpl.hpp" 

namespace Jp 
{ 
     NativeModuleExample::NativeModuleExample(const JSContext& js_context) TITANIUM_NOEXCEPT 
       : JSExportObject(js_context) 
     { 
       TITANIUM_LOG_DEBUG("NativeModuleExample::ctor Initialize"); 
     } 

     void NativeModuleExample::postInitialize(JSObject& js_object) 
     { 
     } 

     void NativeModuleExample::postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments) 
     { 
     } 

     //add new method 
     void NativeModuleExample::aaa() TITANIUM_NOEXCEPT 
     { 
     } 

     void NativeModuleExample::JSExportInitialize() 
     { 
       JSExport<NativeModuleExample>::SetClassVersion(1); 
       JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class()); 
     } 
} 

Fenster/include/JpNativeModuleExample.hpp

#ifndef _JPNATIVEMODULEEXAMPLE_HPP_ 
#define _JPNATIVEMODULEEXAMPLE_HPP_ 

#include "JpNativeModuleExample_EXPORT.h" 
#include "Titanium/detail/TiBase.hpp" 
#include "Titanium/Module.hpp" 

namespace Jp 
{ 
     using namespace HAL; 

     class JPNATIVEMODULEEXAMPLE_EXPORT NativeModuleExample : public JSExportObject, public JSExport<NativeModuleExample> 
     { 
       public: 
         NativeModuleExample(const JSContext&) TITANIUM_NOEXCEPT; 
         void aaa() TITANIUM_NOEXCEPT; 
         virtual void postInitialize(JSObject& js_object) override; 
         virtual void postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments) override; 

         virtual void aaa();  //add new method 

         virtual ~NativeModuleExample()        = default; 
         NativeModuleExample(const NativeModuleExample&)   = default; 
         NativeModuleExample& operator=(const NativeModuleExample&) = default; 
#ifdef TITANIUM_MOVE_CTOR_AND_ASSIGN_DEFAULT_ENABLE 
         NativeModuleExample(NativeModuleExample&&)     = default; 
         NativeModuleExample& operator=(NativeModuleExample&&)  = default; 
#endif 

         static void JSExportInitialize(); 

     }; 
} 
#endif // _JPNATIVEMODULEEXAMPLE_HPP_ 

Beispielcode

var nativemoduleexample = require('jp.NativeModuleExample'); 
Ti.API.info("module is => " + nativemoduleexample); //no problem 
var aaa = nativemoduleexample.aaa(); //titanium error is displayed 

$.index.open(); 

Antwort

1

Um Ihre Funktion zu aktivieren, müssen Sie um es unter Verwendung TITANIUM_ADD_FUNCTION bei JSExportInitialize() zu registrieren. Vielleicht möchten Sie Arbeitsbeispiele unter TitaniumKit wie Ti.UI.Button und Ti.UI.Window überprüfen.

void NativeModuleExample::JSExportInitialize() 
{ 
    JSExport<NativeModuleExample>::SetClassVersion(1); 
    JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class()); 
    TITANIUM_ADD_FUNCTION(NativeModuleExample, aaa); 
} 

Und dann TITANIUM_FUNCION verwenden Funktion wie diese ...

TITANIUM_FUNCTION(NativeModuleExample, aaa) 
{ 
    aaa(); 
    return get_context().CreateUndefined() 
} 
definieren