wpf中使用cefsharp加载本地html网页并实现cs和js的交互,并且cefsharp支持any cpu
废话少说,直接上代码:
第一步:

第二步:

第三步:

第四步:

App.xaml.cs对应的代码:
using CefSharp;
using CefSharp.Wpf;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows; namespace CefSharpe
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
public App()
{
//Add Custom assembly resolver
AppDomain.CurrentDomain.AssemblyResolve += Resolver; //Any CefSharp references have to be in another method with NonInlining
// attribute so the assembly rolver has time to do it's thing.
InitializeCefSharp();
} [MethodImpl(MethodImplOptions.NoInlining)]
private static void InitializeCefSharp()
{
var settings = new CefSettings(); // Set BrowserSubProcessPath based on app bitness at runtime
settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
"CefSharp.BrowserSubprocess.exe"); // Make sure you set performDependencyCheck false
Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
} // Will attempt to load missing assembly from either x86 or x64 subdir
// Required by CefSharp to load the unmanaged dependencies when running using AnyCPU
private static Assembly Resolver(object sender, ResolveEventArgs args)
{
if (args.Name.StartsWith("CefSharp"))
{
string assemblyName = args.Name.Split(new[] { ',' }, )[] + ".dll";
string archSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
assemblyName); return File.Exists(archSpecificPath)
? Assembly.LoadFile(archSpecificPath)
: null;
} return null;
}
}
}
以上实现的是cefsharp支持any cpu
下面实现加载本地html并且实现与js交互,对于如何下载cefsharp就不做过多说明,网上一大把
第五步:

<Window x:Class="CefSharpe.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CefSharpe"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="cefSharp" >
<Grid>
<cefSharp:ChromiumWebBrowser x:Name="cefsharp"></cefSharp:ChromiumWebBrowser>
<Button Height="" VerticalAlignment="Bottom" Click="Button_Click">调用js的方法</Button>
</Grid>
</Window>
第六步:

using CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using CefSharp.Wpf; namespace CefSharpe
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{ InitializeComponent(); CefSharpSettings.LegacyJavascriptBindingEnabled = true; this.cefsharp.Address = AppDomain.CurrentDomain.BaseDirectory+@"index.html";
this.cefsharp.RegisterJsObject("JsObj", new CallbackObjectForJs(), new CefSharp.BindingOptions { CamelCaseJavascriptNames = false});
//阻止默认行为
cefsharp.MenuHandler = new MenuHandler();
}
public class CallbackObjectForJs
{ public string name = "";
public void showTest(string msg)
{
MessageBox.Show(msg);
MessageBox.Show(name);
}
} private void Button_Click(object sender, RoutedEventArgs e)
{
cefsharp.ExecuteScriptAsync("ydb1('asdadaasdasdas我是杨道波')");
}
public class MenuHandler : IContextMenuHandler
{
public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
model.Clear();
}
public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
return false;
}
public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
{
}
public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
} } }
第七步:
在本地项目中放入index.html

第八步:
index.html里面的内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 id="ydb">我是杨道波sdadsad</h1>
<!--<script>--> <!--//屏蔽鼠标右键、Ctrl+N、Shift+F10、F11、F5刷新、退格键-->
<!--document.oncontextmenu = function () { event.returnValue = false; }//屏蔽鼠标右键-->
<!--window.onhelp = function () { return false } //屏蔽F1帮助-->
<!--document.onkeydown = function () {-->
<!--if ((window.event.altKey) &&-->
<!--((window.event.keyCode == ) || //屏蔽 Alt+ 方向键 ←-->
<!--(window.event.keyCode == ))) { //屏蔽 Alt+ 方向键 →-->
<!--event.returnValue = false;-->
<!--return false;-->
<!--}-->
<!--/* 注:这还不是真正地屏蔽Alt+方向键,-->
<!--因为Alt+方向键弹出警告框时,按住Alt键不放,-->
<!--用鼠标点掉警告框,这种屏蔽方法就失效了。*/-->
<!--if ((event.keyCode == ) || //屏蔽退格删除键-->
<!--(event.keyCode == ) || //屏蔽 F5 刷新键-->
<!--(event.ctrlKe && event.keyCode == )) { //Ctrl + R-->
<!--event.keyCode = ;-->
<!--event.returnValue = false;-->
<!--}-->
<!--if (event.keyCode == ) { event.keyCode = ; event.returnValue = false; } //屏蔽F11-->
<!--if (event.ctrlKey && event.keyCode == ) event.returnValue = false; //屏蔽Ctrl+n-->
<!--if (event.shiftKey && event.keyCode == ) event.returnValue = false; //屏蔽shift+F10-->
<!--if (window.event.srcElement.tagName == "A" && window.event.shiftKey)-->
<!--window.event.returnValue = false; //屏蔽shift加鼠标左键新开一网页-->
<!--if ((window.event.altKey) && (window.event.keyCode == )) { //屏蔽Alt+F4-->
<!--window.showModelessDialog("about:blank", "", "dialogWidth:1px;dialogheight:1px");-->
<!--return false;-->
<!--}-->
<!--}-->
<!--</script>--> <script>
ydb.onclick = function(){
JsObj.name = "asdsadasdas"; JsObj.showTest("test");
}
function ydb1(msg) { alert(msg) }
</script>
</body>
</html>
效果展示:

点击文字:

点击名为(调用js的方法)的按钮:

拿走不谢.
这是要把我一个学前端逼的转行的节奏啊.
wpf中使用cefsharp加载本地html网页并实现cs和js的交互,并且cefsharp支持any cpu的更多相关文章
- .net core Wpf中使用cefsharp加载本地html网页,并且cefsharp支持any cpu
第一步,在程序包管理器安装 cefsharp.wpf 第二步 您必须在项目的第一个 < propertygroup > 中添加 < cefsharpanycpusupport > ...
- WPF 中图片的加载 ,使用统一资源标识符 (URI)
在wpf中,设置图片路径有2种方法: 1.xaml文件中,指定路径 <Button Name=" HorizontalAlignment="Right" Verti ...
- WP8.1程序开发中,如何加载本地文件资源或安装在程序包中的资源。
Web 要访问来自 Web 的文件,你可以使用标准的绝对 HTTP URI: <img src="http://www.contoso.com/images/logo.png" ...
- 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图
[源码下载] 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Co ...
- worker中加载本地文件报错的解决方案
如果在一个swf的主线程中加载文件时,报安全沙箱的错误, 网上有诸多的解决方案.但是如果在一个worker中加载本地文件报类似如下的错误: *** 安全沙箱冲突 *** SecurityError: ...
- 重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图
原文:重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 Web ...
- angular 图片加载失败 情况处理? 如何在ionic中加载本地图片 ?
1.angular 图片加载失败 情况处理 在directive中定义组件,在ng-src错误时,调用err-src app.directive('errSrc',function(){ return ...
- Android 中加载本地Html 跨域问题,http协议允许加载
一.需求: 后台加载HTML的包时间太长,太卡,让把所有的HTML包放到前台:使用的是file://协议,有些内容和样式加载不出来,H5那边说需要用http://协议来加载: 二.处理过程: 安卓最简 ...
- #iOS问题记录#动态Html加载本地CSS和JS文件
所谓动态Html,指代码中组合生成的html字符串: 若需要加载本地CSS,图片,JS文件,则, 1,需要文件的全路径: 2,需要"file:///"标志: 例如: //获取文件全 ...
随机推荐
- 拖动条SeekBar
1TextView tv=(TextView)findViewById(R.id.TV); 2 tv.setMovementMethod(ScrollingMovementMethod.getInst ...
- [小北De编程手记] : Lesson 04 - Selenium For C# 之 API 上
这一部分,我准备向大家介绍Selenium WebDriver的常用API,学习这部分内容需要大家最好有一些简单的HTML相关知识,本文主要涉及到以下内容: Selenium API:元素检查 Sel ...
- 【Leetcode】【Medium】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- QT的组件布局
在QT的IDE下,编写一个自定义布局. #include<QApplication> #include<QWidget> #include<QSpinBox> #i ...
- 404错误 标签: servlet浏览器 2016-11-16 16:58 61人阅读 评论(0) 收藏
404是资源没有找到,一般由于以下几个方面导致: 1.路径出错: a)检查web.xml中servlet的配置是否出错 b)浏览器访问是路径书写方式:http://localhost:8080/项目名 ...
- ACM HDU-2952 Counting Sheep
Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- dailiaojie new
http://imushan.com/categories/Java/ 编译优化手段.
- LINQPad 编译调试C#代码的工具推荐
LinqPad介绍 学习C#代码的好帮手,很容易调试C#代码片段. LINQPad 4 支持.NET Framework 4.0 / 4.5 ,专业调试LINQ,lambda等特性,完全取代Snipp ...
- SAP S/4HANA生产订单创建时使用的工厂数据是从什么地方带出来的
大家如果使用我github上的这段代码创建S/4HANA的生产订单时,一定会发现,我在代码里并没有硬编码来指定生产订单的ID,然而运行时会发现我在系统里配置的这个2800被自动使用了,这是怎么做到的呢 ...
- WAS上配置数据源连接失败
问题描述: 在节点 cnshh171Node01 上的服务器 server1 上, 对数据源 testj2cbug 执行的测试连接操作 由于以下异常 java.sql.SQLException: 调用 ...