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方法。
4)自定义js函数还有一种异步注册js方式和带参数的调用方式,具体的方法请问度娘啦。
大家在图片中看到这样的域名访问custom://cefsharp/zpy.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
});
以下是注册本地的html文件的,这样就可以通过自定义的url访问本地文件。
static CefSharpSchemeHandlerFactory()
{
ResourceDictionary = new Dictionary
{
{ "/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 }
};
}