如何调用添加到Appelerator平台的Windows本机模块的方法

时间:2016-03-25 04:19:24

标签: windows appcelerator appcelerator-titanium

我们写信询问如何正确调用Titanium Appelerator本机模块的方法。 我们的开发环境如下:

  

Titanium SDK:5.2.0
  Appcelerator CLI:5.2.0
  的TabletPC:Windows10pro

我们的模块是按照以下程序开发的;

(1)我们创建了一个参考https://github.com/appcelerator/titanium_mobile_windows#module-development文档的模块 另外一个方法aaa()被添加到模块中。

#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());
        }
}

(2)创建了一个windows'应用程序来调用上面的模块,当我们从应用程序调用模块时没有错误。但是,当我们调用方法aaa()时,发生以下Titanium错误消息; “aaa()方法不存在”。

在用于windows10的Titanium windows平台中,模块的dll已正确创建。然后,我们只是想知道我们来源的哪些部分不正确;

(1)方法aaa()的定义不正确。 (2)调用本机模块的源代码不正确。 (3)调用aaa()方法的源代码不正确。

我们附上了以下源代码,并提前感谢您的意见和建议。

代码如下。

窗/ SRC / JpNativeModuleExample.cpp

#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_

窗/包含/ JpNativeModuleExample.hpp

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

$.index.open();

示例代码

{{1}}

1 个答案:

答案 0 :(得分:1)

要启用您的功能,您需要使用TITANIUM_ADD_FUNCTION JSExportInitialize()进行注册。您可能需要查看TitaniumKit下的工作示例,例如Ti.UI.ButtonTi.UI.Window

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

然后使用TITANIUM_FUNCION来定义这样的函数......

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