我已经冲刷互联网寻找一个答案,但我一定要问错了问题。 我有与Awesomium网络控制一个C#WinForms应用程序。 我能够调用在加载的页面中应用程序中定义的JavaScript方法? 如果是这样,怎么样? (样品js代码将不胜感激)。 谢谢!
Answer 1:
该方法依赖于它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版
Form.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");
}
Answer 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>
Answer 3:
对于新版本
从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()");
文章来源: Can I call application methods from JavaScript in Awesomium?