Awesomium允许我在JS中调用/使用C ++变量/方法吗?

Awesomium很容易让C ++代码调用Javascript方法,但我没有找到一个明确的答案,如果它可以做相反的事情。这个网站似乎说你可以,但通过文字和示例查看并不能启发我。 所以,我正在寻找一个明确的答案:我可以在我的Javascript(Jquery)中调用C ++变量/方法吗? 如果你可以包含一个简单的例子,那将非常受欢迎。 谢谢!     
已邀请:
你绝对可以 - 你只需要使用delegates / function-pointers在WebView :: setObjectCallback和WebViewListener :: onCallback之上构建一个额外的层。 我编写了一个快速JSDelegate.h类(在此处查看),您可以使用它将“onCallback”事件直接连接到C ++成员函数。 基本思想是维护回调名称到委托的映射:
typedef std::map<std::wstring, Awesomium::JSDelegate> DelegateMap;
DelegateMap _delegateMap;
并从WebViewListener :: onCallback调用相应的函数:
void MyListener::onCallback(Awesomium::WebView* caller, const std::wstring& objectName, 
    const std::wstring& callbackName, const Awesomium::JSArguments& args)
{
    DelegateMap::iterator i = _delegateMap.find(callbackName);

    if(i != _delegateMap.end())
        i->second(caller, args);
}
然后,每次你想要绑定一个特定的C ++函数时,你会这样做:
// Member function we wish to bind, must have this signature for JSDelegate
void MyClass::myFunction(Awesomium::WebView* caller, const Awesomium::JSArguments& args)
{
    // handle args here
}

// Instantiate MyClass instance in C++
MyClass* myClass = new MyClass();

// Create corresponding 'MyClass' object in Javascript
webView->createObject(L"MyClass");

// Do the following for each member function:    
// Bind MyClass::myFunction delegate to MyClass.myFunction in JS
_delegateMap[L"myFunction"] = Awesomium::JSDelegate(myClass, &MyClass::myFunction);
webView->setObjectCallback(L"MyClass", L"myFunction");
然后,您应该能够直接从Javascript调用MyClass :: myFunction,如下所示:
MyClass.myFunction("foo", 1, 2 3)
希望这可以帮助!我没有测试过任何代码,但是我用Awesomium v​​1.6 RC4 SDK编写了它。     

要回复问题请先登录注册