我在互联网上寻找答案,但我一定是在问错误的问题。我有一个带有Awesomium网页控件的C#winforms应用程序。我能够从加载的页面中的javascript调用应用程序中定义的方法吗?如果是这样,怎么样? (样本js代码将不胜感激)。谢谢!
答案 0 :(得分:31)
该方法取决于您使用的Awesomium版本。在即将发布的1.7版本(目前为1.7 RC3)中如何完成以及如何完成之前已经有了一些变化。请注意,1.7中有一个改进,因为当JS调用应用程序上的方法时,.NET方法现在可以返回值。我不相信这在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");
}
答案 1 :(得分: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>
答案 2 :(得分: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()");