我是整个cefsharp浏览器通信的新手,并且我的浏览器正在加载我的自定义html js和richtextbox。
我需要在这两者之间进行通信,如果有人在Richtextbox中输入内容,它将自动将其作为变量发送到浏览器,我的自定义JS将在其中获取内容,同样,如果有人在浏览器中输入内容,则js函数将将其作为变量发送到c#,这将更新richtextbox。
将变量从js发送到c#正在使用RegisterAsyncJSObject工作,但是当上述工作完成并更新我的Richtextbox,并且在Richtextbox中输入内容后,ExecuteScriptAsync不起作用,但是如果我删除了所有< [RegisterAsyncJSObject已在richtextbox中更新,并向richtextbox输入了一些内容,然后ExecuteScriptAsync起作用
这里是代码创建AsyncJSObject:
mw = ((MainWindow)System.Windows.Application.Current.MainWindow);
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
MainWindowBrowser.RegisterAsyncJsObject("getTEXTfromjs", mw);
JS代码
getHTMLfromjs.updateonDrag(result);
AsycnJSObject更新richtextbox的代码:
public void updateonBrowserEnter(String rtbHolder) {
this.Dispatcher.Invoke(() => {
richtextbox.Text = rtbHolder;
});
}
Richtextbox TextChanged上的ExecuteScriptAsync代码:
String rtbh = richtextbox.Text;
MainWindowBrowser.ExecuteScriptAsync(String.Format("testFunc('{0}');", rtbh));
第二个问题是C#与js之间的通信是我执行时
frame.ExecuteJavaScriptAsync(String.Format("testFunc('{0}')", myvar));
J似乎似乎不接受带有单引号('')的多行字符串,因此在我的情况下,我有两个选择]]
`
并解决了问题