wpf RegisterJsObject CefSharp 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28473978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:56:47  来源:igfitidea点击:

RegisterJsObject CefSharp not work

javascriptc#wpfcefsharp

提问by salvo italy

I have that problem whith CefSharp and js communication. This is my structure. In my MainWindow.xaml.cs I have:

我有 CefSharp 和 js 通信的问题。这是我的结构。在我的 MainWindow.xaml.cs 我有:

chromium = new CefSharp.Wpf.ChromiumWebBrowser();
chromium.RegisterJsObject("jsInterface", new jsMapInterface());

where jsMapInterfaceis a public class with its methods.

其中jsMapInterface是一个带有方法的公共类。

In my html/js folder, I call a C# method with:

在我的 html/js 文件夹中,我调用了一个 C# 方法:

window.jsInterface.myTest();
//jsInterface.myTest();  //I have tested twice

where myTest() is a method inside jsMapInterfaceclass but nothing happens. Where is my mistake?

其中 myTest() 是jsMapInterface类中的一个方法,但没有任何反应。我的错误在哪里?

Thanks in advise.

谢谢指教。

回答by jornh

Try UpperCasing on the C# side e.g MyTest()as mentioned under Please notein https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#3-how-do-you-expose-a-net-class-to-javascript

尝试在C#侧例如UpperCasingMyTest()作为中提到请注意https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#3-how-do-you-expose-a-net-class-to -javascript

CefSharp does perform name mangling. The reason is it tries to encourage following JS conventions on the JS side and C# conventions on that side.

CefSharp 确实执行名称修改。原因是它试图鼓励遵循 JS 方面的 JS 约定和那方面的 C# 约定。

Finally work from the example code at https://github.com/cefsharp/CefSharp/search?utf8=%E2%9C%93&q=BoundObject

最后从https://github.com/cefsharp/CefSharp/search?utf8=%E2%9C%93&q=BoundObject 上的示例代码开始工作

回答by penderi

Hi I guess I may be late on this... but everything looks like it's wired up fine to me... but maybe you should keep a modular ref to new jsMapInterface()to ensure it's not destroyed/cleaned up.

嗨,我想我可能会迟到......但一切看起来对我来说都很好......但也许你应该保留一个模块化参考new jsMapInterface()以确保它不会被破坏/清理。