简体   繁体   中英

Awesomium - Return Javascript Result to C#

Javascript / HTML Code

<script type="text/javascript">
function configurator(clicked) { 
return clicked.name;   
} 
</script>

<a name="link1" href="#" onclick="configurator(this)">Link 1</a>
<a name="link2" href="#" onclick="configurator(this)">Link 2</a>

I want to return the "clicked.name" value to let's say "result" variable in C# when onclick event on the link triggered. I don't know how to do that. Could someone help me...

Note: I'm still very new to Awesomium :)

Update:

I follow @JonnyReeves method with a little modified:

using (JSObject myGlobalObject = webControl1.CreateGlobalJavascriptObject("myGlobalObject"))
{
    myGlobalObject.Bind("onLinkClicked", true, (sen, eve) =>
    {
        MessageBox.Show(Convert.ToString(sen));
    });
}

but I got "Awesomium.Windows.Controls.WebControl" as result not the clicked link's name.

It looks like you you could make use of Awesomium's JSObject.bind method to invoke a C# method from JavaScript. A typical approach would be to expose a JavaScript global object which includes all your 'bridge' methods (ie: those that pass data between C# and JavaScript).

// Create and acquire a Global Javascript object.
// These object persist for the lifetime of the web-view.
using ( JSObject myGlobalObject = webView.CreateGlobalJavascriptObject( "myGlobalObject" ) )
{
    // The handler is of type JavascriptMethodEventHandler. Here we define it
    // using a lambda expression.
    myGlobalObject.Bind( "onLinkClicked", false, ( name ) =>
    {
        Debug.Print( String.Format( "User clicked: {0}", name ) );
    } );
}

You can now invoke this method from your JavaScript code:

<script type="text/javascript">
    function configurator(clicked) 
    { 
           myGlobalObject.onLinkClicked(clicked.name);
    } 
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM