如何使用cefsharp将输入传递给javascript函数 [英] How to pass inputs to javascript functions with cefsharp

查看:626
本文介绍了如何使用cefsharp将输入传递给javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将CefSharp WebView添加到我的WPF应用程序中,以代替我们使用的原始WebBrowsers。 WebBrowser具有InvokeScript函数( http:// msdn .microsoft.com / en-us / library / cc452443(v = vs.110).aspx )允许您调用JavaScript函数,还可以选择将输入的obj数组传递给该JS函数。

I am trying to add a CefSharp WebView to my WPF application in place of the original WebBrowsers that we used. The WebBrowser has an InvokeScript function (http://msdn.microsoft.com/en-us/library/cc452443(v=vs.110).aspx) Which allows you to invoke a JavaScript function and optionally pass in an obj array of inputs to that JS function.

有什么方法可以与CefSharp WebView做类似的事情,在那里我可以将输入参数传递给JavaScript函数?例如,我可以这样做:

Is there any way to do something similar with the CefSharp WebView where I can pass input parameters to the JavaScript function? For example, I can do:

this.webBrowser.InvokeScript("scriptName", input0, input1, input2);

使用WebBrowser,是否有任何等效功能或多个功能,可以通过CefSharp WebView?

with the WebBrowser, is there any equivalent function, or multiple functions, that would allow for this with the CefSharp WebView?

推荐答案

我弄清楚了如何做自己想做的事情……而且相对简单。

I figured out how to do what I wanted... and it was relatively straightforward.

如果需要WebBrowser的InvokeScript函数的功能

If you want the functionality of the WebBrowser's InvokeScript function

this.webBrowser.InvokeScript("functionName", input0, input1, input2);

使用CefSharp WebView,然后您只需要执行以下操作即可:

with the CefSharp WebView then you just have to do something like this:

webView.ExecuteScript(String.Format("functionName({0},{1},{2});", input0, input1, input2));

由于您要调用javascript函数,因此必须确保正确转义任何字符串参数。输入将使用您的值填充,因此将被视为字符串文字。

You will have to make sure to escape any string parameters correctly since you are calling a javascript function where the inputs will be filled in with your values and therefore will be treated as string literals.

如果您真的不想更改代码,而要换出WebBrowser使用WebView,则可以制作一个扩展方法,将 InvokeScript添加到WebView。

If you really don't want to change your code and you are swapping out the WebBrowser with a WebView then you could make an extension method that adds "InvokeScript" to the WebView.

这篇关于如何使用cefsharp将输入传递给javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆