【转载】 https://blog.csdn.net/zpyxman/article/details/78779291
本章主要讲解自定义javascript函数进行本地调用和自定义本地域名。1、自定义javascript函数1)得到browser对象然后注册自己的js对象。如下: //注册usbjs对象browser.RegisterJsObject("usbKey", new UsbKeyBound());//注意在js函数里面只驼峰写法开头,如果要使用C#写法就使用下面的注册方式。//BindingOptions bo = new BindingOptions();//bo.CamelCaseJavascriptNames = false;//browser.RegisterJsObject("usbKey", new UsbKeyBound(), bo);2) usbkey就是js对象的名字,下面我们来看看UsbKeyBound中的内容。 public class UsbKeyBound{ public void ShowTest() { MessageBox.Show("这个一个测试"); }}3)下面来看看在html中怎么调用自定义的js方法。 <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title></head><body><button type="button">点击这里</button></body></html><script type="text/javascript">function test() { //注意在js函数里面只认小写开头 usbKey.showTest();}</script>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
效果如下图:
4)自定义js函数还有一种异步注册js方式和带参数的调用方式,具体的方法请问度娘啦。 大家在图片中看到这样的域名访问custom://cefsharp/zpy.html,都想知道怎么怎么自己注册的,下面我们来看看怎么注册自定义域名,并且访问本地html文件。 注册本地html文件在settings中这行代码注册了Scheme: settings.RegisterScheme(new CefCustomScheme { //CefSharpSchemeHandlerFactory.SchemeName的值是"custom" SchemeName = CefSharpSchemeHandlerFactory.SchemeName, SchemeHandlerFactory = new CefSharpSchemeHandlerFactory(), IsSecure = true //treated with the same security rules as those applied to "https" URLs });来看看CefSharpSchemeHandlerFactory以下是注册本地的html文件的,这样就可以通过自定义的url访问本地文件。 static CefSharpSchemeHandlerFactory() { ResourceDictionary = new Dictionary<string, string> { { "/home.html", Resources.home_html }, { "/assets/css/shCore.css", Resources.assets_css_shCore_css }, { "/assets/css/shCoreDefault.css", Resources.assets_css_shCoreDefault_css }, { "/assets/css/docs.css", Resources.assets_css_docs_css }, { "/assets/js/application.js", Resources.assets_js_application_js }, { "/assets/js/jquery.js", Resources.assets_js_jquery_js }, { "/assets/js/shBrushCSharp.js", Resources.assets_js_shBrushCSharp_js }, { "/assets/js/shBrushJScript.js", Resources.assets_js_shBrushJScript_js }, { "/assets/js/shCore.js", Resources.assets_js_shCore_js }, { "/bootstrap/bootstrap-theme.min.css", Resources.bootstrap_theme_min_css }, { "/bootstrap/bootstrap.min.css", Resources.bootstrap_min_css }, { "/bootstrap/bootstrap.min.js", Resources.bootstrap_min_js }, { "/BindingTest.html", Resources.BindingTest }, { "/ExceptionTest.html", Resources.ExceptionTest }, { "/PopupTest.html", Resources.PopupTest }, { "/SchemeTest.html", Resources.SchemeTest }, { "/TooltipTest.html", Resources.TooltipTest }, { "/FramedWebGLTest.html", Resources.FramedWebGLTest }, { "/MultiBindingTest.html", Resources.MultiBindingTest }, { "/ScriptedMethodsTest.html", Resources.ScriptedMethodsTest }, { "/ResponseFilterTest.html", Resources.ResponseFilterTest }, { "/DraggableRegionTest.html", Resources.DraggableRegionTest }, { "/CssAnimationTest.html", Resources.CssAnimation }, { "/CdmSupportTest.html", Resources.CdmSupportTest }, { "/Recaptcha.html", Resources.Recaptcha }, { "/zpy.html", Resources.zpy } }; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
以下是资源文件所在目录:
|
|