我可以在 Awesomium 中从 JavaScript 调用应用程序方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13280727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:17:44  来源:igfitidea点击:

Can I call application methods from JavaScript in Awesomium?

javascriptawesomium

提问by Daniel Szabo

I've scoured the internet looking for an answer, but I must be asking the wrong question. I have a C# winforms app with an Awesomium web control. Am I able to call methods defined in the app from javascript in the page that loads? If so, how? (sample js code would be greatly appreciated). Thanks!

我在互联网上搜索寻找答案,但我一定问错了问题。我有一个带有 Awesomium 网络控件的 C# winforms 应用程序。我可以从加载的页面中的 javascript 调用应用程序中定义的方法吗?如果是这样,如何?(示例 js 代码将不胜感激)。谢谢!

回答by HotN

The approach depends on which version of Awesomium you're using. There's been a bit of a change of how this is done in the upcoming version 1.7 (currently at 1.7 RC3) and how it was done before. Note that there's one improvement in 1.7, in that .NET methods can now return values when JS calls a method on your app. I don't believe this was possible prior to 1.7.

该方法取决于您使用的 Awesomium 版本。在即将发布的 1.7 版(目前为 1.7 RC3)中,这方面的工作方式以及之前的方式发生了一些变化。请注意,1.7 中有一项改进,即 .NET 方法现在可以在 JS 调用应用程序上的方法时返回值。我不相信这在 1.7 之前是可能的。

Here are the two methods:

下面是两种方法:

test.html (used for both versions)

test.html(用于两个版本)

...
<script type="text/javascript">

    function myMethod() {
        document.write("In myMethod, calling .NET but expecting no return value.<br/>");

        jsobject.callNETNoReturn();
    }

    function myMethodExpectingReturn() {
        document.write("In myMethodExpectingReturn, calling .NET and expecting return value.<br/>");

        var returnVal2 = jsobject.callNETWithReturn("foo");
        document.write("Got value from .NET: " + returnVal2 + "<br/>");
    }

    function myMethodProvidingReturn(whatToReturn) {
        var returnVal =  whatToReturn + "bar";
        document.write("Returning '" + returnVal + "' to .NET.");

        return returnVal;
    }
</script>
...

Version 1.7

版本 1.7

Form1.cs

表格1.cs

public Form1()
{
    InitializeComponent();

    //webView is an instance of WebControl defined in your form
    webView.DocumentReady += WebViewOnDocumentReady;

    webView.Source = new Uri("test.html");
}

private void WebViewOnDocumentReady(object sender, UrlEventArgs urlEventArgs)
{
    webView.DocumentReady -= WebViewOnDocumentReady;

    JSObject jsobject = webView.CreateGlobalJavascriptObject("jsobject");

    jsobject.Bind("callNETNoReturn", false, JSHandler);
    jsobject.Bind("callNETWithReturn", true, JSHandler);

    webView.ExecuteJavascript("myMethod()");
    webView.ExecuteJavascript("myMethodExpectingReturn()");
    var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");
    Console.WriteLine(result.ToString());
}

private void JSHandler(object sender, JavascriptMethodEventArgs args)
{
    if (args.MustReturnValue)
    {
        Console.WriteLine("Got method call with return request");
        args.Result = "Returning " + args.Arguments[0];
    }
    else
    {
        Console.WriteLine("Got method call with no return request");
    }
}

Version 1.6

版本 1.6

Form.cs

表单.cs

public Form1()
{
    InitializeComponent();

    //webView is an instance of WebControl defined in your form
    webView.DomReady += WebViewOnDomReady;

    webView.Source = new Uri("test.html");
}

private void WebViewOnDomReady(object sender, EventArgs eventArgs)
{
    webView.DomReady -= WebViewOnDomReady;

    webView.CreateObject("jsobject");
    webView.SetObjectCallback("jsobject", "callNETNoReturn", JSHandler);

    webView.ExecuteJavascript("myMethod()");

    var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");
    Console.WriteLine(result.ToString());
}

private void JSHandler(object sender, JSCallbackEventArgs args)
{
    Console.WriteLine("Got method call with no return request");
}

回答by sirbrialliance

In C++: (the .NET bindings will likely be similar)

在 C++ 中:(.NET 绑定可能是类似的)

Define a callback class:

定义回调类:

class TestListener : public Awesomium::WebViewListener {
public:
    virtual void onCallback(
        Awesomium::WebView* caller,
        const std::wstring& objectName,
        const std::wstring& callbackName,
        const Awesomium::JSArguments& args
    ) {
        if (objectName == L"myApi" && callbackName == L"doMagicFoo") {
            cout << "callback called with " << args.size() << " args\n";
        }
    }

    //...implement all the other pure virtual functions...  
};

Then as you set up your WebView:

然后在设置 WebView 时:

TestListener bob;
webView->setListener(&bob);
webView->createObject(L"myApi");
webView->setObjectCallback(L"myApi", L"doMagicFoo");

Then in your HTML/JS:

然后在你的 HTML/JS 中:

<button onclick="myApi.doMagicFoo('super', 45)">do native call</button>

回答by PJ3

For newer versions

对于较新的版本

Calling from JavaScript:

从 JavaScript 调用:

webControl1.LoadingFrameComplete += LoadingFramecompleted;

public void LoadingFramecompleted(object sender, FrameEventArgs e){
    //after loading complete create global object
    JSObject obj = webControl1.CreateGlobalJavascriptObject("jsobject");
    obj.Bind(myMethod);
}

private JSValue myMethod(object sender, JavascriptMethodEventArgs e)
{
    MessageBox.Show("hello world");
    return "My response";
}

Inside JavaScript

JavaScript 内部

jsobject.myMethod(); //myMethod is the method name defined in c#

To Call JavaScript

调用 JavaScript

webControl1.ExecuteJavascript("SayHello()");