Returning value to C# function from Javascript not working in Awesomium

本秂侑毒 提交于 2019-12-25 05:53:21

问题


My previous question went unaswered; I've made some progress in narrowing down the issue, but I only have a week left in my summer student work term and I would like to resolve this issue before I go.

I currently want to return a value from a Javascript function back into C# in Awesomium. Below is the implementation of how I do it in my C# application.

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

        webView.CreateObject("jsobject");
        //webView.SetObjectCallback("jsobject", "callNETNoReturn", JSHandler);
        webView.SetObjectCallback("jsobject", "callNETWithReturn", JSHandler);
        //webView.ExecuteJavascript("myMethod()");
        var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");
        MessageBox.Show("Stuff:" + result.ToString());
    }

    private void JSHandler(object sender, JSCallbackEventArgs args)
    {
        var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");

        MessageBox.Show(result.ToString());
        MessageBox.Show("Got method call with no return request");
    }

The Javascript functions in my .HTML file look like this:

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

I have narrowed it down to depending on what I load into the webview in Awesomium as the issue, using webView.LoadHTML(myString);

When myString is set to a file's contents that only contain

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

myMethodExpectingReturn and myMethodProvidingReturn(whatToReturn) return properly. However, when I put these methods inside my actual .HTML file, it seems to call the event handler over and over again, freezing up my program (this was verified by printing messageboxes).

What could possibly cause this looping behaviour? I'll refrain from posting my entire .HTML file unless necessary.

Thanks

来源:https://stackoverflow.com/questions/25456684/returning-value-to-c-sharp-function-from-javascript-not-working-in-awesomium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!