Returning value to C# function from Javascript not

2019-09-17 09:51发布

问题:

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