最近有一个需求是,WPF里面要嵌入一个Vue前端框架,也就是把网页嵌入进WPF里面,找了好久发现用CefSharp还是比较不错的,但是有一点打包占空间太大

这是第一种解法:

    public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CefSharpSettings.LegacyJavascriptBindingEnabled = true; //this.cefsharp.Address = AppDomain.CurrentDomain.BaseDirectory + @"index.html";
this.cefsharp.RegisterJsObject("jsobjp", new CallbackObjectForJs(), new CefSharp.BindingOptions { CamelCaseJavascriptNames = false });
//阻止默认行为
cefsharp.MenuHandler = new MenuHandler();
this.cefsharp.LifeSpanHandler = new OpenPageSelf();
} public class CallbackObjectForJs
{ public string name = "";
public void send(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;
}
} /// <summary>
/// 在自己窗口打开链接
/// </summary>
internal class OpenPageSelf : ILifeSpanHandler
{
public bool DoClose(IWebBrowser browserControl, IBrowser browser)
{
return false;
} public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
{ } public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
{ } public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
newBrowser = null;
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
chromiumWebBrowser.Load(targetUrl);
return true; //Return true to cancel the popup creation copyright by codebye.com.
}
}
}
    xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
<Grid>
<cefSharp:ChromiumWebBrowser x:Name="cefsharp" Address="http://127.0.0.1:8081/" />
<Button
Height="30"
VerticalAlignment="Bottom"
Click="Button_Click">
调用js的方法
</Button>
</Grid>
    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[] { ',' }, 2)[0] + ".dll";
string archSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
assemblyName); return File.Exists(archSpecificPath)
? Assembly.LoadFile(archSpecificPath)
: null;
} return null;
}
}

第二种解法:

https://docs.microsoft.com/zh-cn/microsoft-edge/webview2/get-started/wpf

请详细看官方文档,里面也讲述了如何嵌入网页,并从网页返回消息给 WPF

具体代码

wpf & javascript & web的更多相关文章

  1. 逆天通用水印支持Winform,WPF,Web,WP,Win10。支持位置选择(9个位置 ==》[X])

    常用技能:http://www.cnblogs.com/dunitian/p/4822808.html#skill 逆天博客:http://dnt.dkil.net 逆天通用水印扩展篇~新增剪贴板系列 ...

  2. [Javascript] Web APIs: Persisting browser data with window.localStorage

    Local Storage is a native JavaScript Web API that makes it easy to store and persist data (as key-va ...

  3. Dynamics CRM2016 升级老版本报“JavaScript Web 资源包含对 Microsoft Dynamics CRM 4.0 (2007) Web 服务终结点的引用”问题的解决办法

    今天在新的服务器上部署了CRM2016 on-premises,并将CRM2015的数据库拷贝过来准备附加后升级,但在升级过程中遇到了如下错误,向导检测到了我的JavaScript Web 资源中包含 ...

  4. 推荐10款优秀的JavaScript Web UI库 框架和套件

    在进行Web开发时,并非所有的库都适合你的项目,但真正开发的时候,你任然需要依赖一款UI框架.特别在你时间紧迫的时候,它是你忠实的朋友. 他们都是些广泛使用包含不同语言实现的WEB UI框架.今天我就 ...

  5. JavaScript Web 应用最佳实践分析

    [编者按]本文作者为 Mathias Schäfer,旨在回顾在客户端大量使用JavaScript 的最佳 Web应用实践.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 对笔者来说,Jav ...

  6. 试读《基于MVC的JavaScript Web富应用开发》— 不一样的JavaScript

    前言 <基于MVC的JavaScript Web富应用开发>是ItEye在7月份发起试读的书.下载了试读的章节,发现只有全本的开始到第二章,第一章很简洁明了地讲述了JavaScript的历 ...

  7. 【WIP】客户端JavaScript Web Object

    创建: 2017/10/11   更新: 2017/10/14 标题加上[WIP],增加[TODO] 更新: 2018/01/22 更改标题 [客户端JavaScript Web Object, UR ...

  8. Dynamics CRM2016 升级老版本号报“JavaScript Web 资源包括对 Microsoft Dynamics CRM 4.0 (2007) Web 服务终结点的引用”问题的解决的方法

    今天在新的server上部署了CRM2016 on-premises,并将CRM2015的数据库拷贝过来准备附加后升级,但在升级过程中遇到了例如以下错误.向导检測到了我的JavaScript Web ...

  9. 10个优秀的JavaScript Web UI库/框架推荐

    在进行Web开发时,并非所有的库都适合你的项目,但你仍需要收藏一些Web UI设计相关的库或框架,以在你需要的时候,加快你的开发效率. 本文为你带来10款非常优秀的基于JavaScript的Web U ...

随机推荐

  1. mysql:insert replace

    在使用SQL语句进行数据表插入insert操作时,如果表中定义了主键,插入具有相同主键的记录会报错: Error Code: 1062. Duplicate entry 'XXXXX' for key ...

  2. 何为“Secure Contexts”安全内容? 终于说明白了!

    ​​​何为"Secure Contexts"安全内容? 终于说明白了! 看图说话 [途径1]:地址栏输入: edge://flags/ 按需设置选项后,重启浏览器即可. Allow ...

  3. 在NestJS 中添加对Stripe 的WebHook 验证

    在NestJS 中添加对Stripe 的WebHook 验证 背景介绍 Nest 是一个用于构建高效,可扩展的NodeJS 服务器端应用程序的框架.它使用渐进式JavaScript, 内置并完全支持T ...

  4. Deepin 20.2.2 /UOS 20.2 添加ppa源

    由于 工作需要,需要通过PPA安装一些优质的软件包,但是 Deepin 默认不支持PPA源 解决方法 由于Deepin/Uos系统默认是没有安装PPA的那么我们得先安装PPA来支持"add- ...

  5. tomcat与springmvc 结合 之---第18篇 StandContext容器和SpringMVC的监听器的模型

    writedby张艳涛 如何使用tomcat的监听器 public class BootStrap_ex06 { public static void main(String[] args) { Ht ...

  6. 图解java多线程设计模式之一一synchronized实例方法体

    synchronized实例方法体和synchronized代码块 synchronied void method(){ ....... } 这个等同于下面将方法体用synchronized(this ...

  7. C#曲线分析平台的制作(三,三层构架+echarts显示)

    本文依据CSDN另一位网友关于三层构架的简单搭建,基于他的源码进行修改.实现了三层构架合理结构,以及从数据库中传递数值在echarts显示的实验目的. 废话不多说,show me codes: 具体构 ...

  8. jquery 对HTML标签的克隆、删除

    <table width="100%" class="table_form"> <tr> <td>奖励深度(<a hr ...

  9. CentOS 7 安装虚拟机

    1.本次安装centos7 安装使用的软件是VitrualBox 虚拟机软件 Oracle公司的虚拟机软件,免费商品(大家可以百度搜索去官网下载) 1:我这里使用的是阿里的centos7的镜像(大家可 ...

  10. AlarmManager定时提醒的那些坑

    https://blog.csdn.net/zackratos/article/details/53243595 https://blog.csdn.net/bingshushu/article/de ...