15

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

4

3 回答 3

32

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

以下是两种方法:

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>
...

1.7 版

Form1.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");
    }
}

1.6版

表格.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");
}
于 2012-11-14T16:15:48.477 回答
2

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

定义一个回调类:

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...  
};

然后在设置 WebView 时:

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

然后在您的 HTML/JS 中:

<button onclick="myApi.doMagicFoo('super', 45)">do native call</button>
于 2012-11-10T23:04:57.820 回答
0

对于较新的版本

从 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";
}

JavaScript 内部

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

调用 JavaScript

webControl1.ExecuteJavascript("SayHello()");
于 2016-07-20T14:38:09.510 回答