https://www.codeproject.com/articles/881315/display-html-in-wpf-and-cefsharp-tutorial-part

Introduction

There are not too many choices when it comes to displaying HTML inside of a WPF application without mixing code with COM or other plugin technologies. And there are even less projects when it comes to open source and freedom of usage.

The CefSharp Version 3 project is a very interesting and stable project. Be sure to read the Wiki and FAQ section before getting started or reporting a problem [3].

What I am missing though, is a good tutorial that guides me from simple scenario into the complex world of WPF and MVVM. This series of articles is an attempt at contributing documentation to change this for good.

Prerequisites

This article series requires not much more than Visual Studio Express and in an Internet connection. So, lets start it up and lets create our first WPF solution name Sample1. Please be sure to set the minimum .Net version for the project to be at least .Net 4.5.2.

Lets go to: Visual Studio > File > New Project ... and enter the required parameters

to create the project.

Creating a WPF Solution with CefSharp Version 3

We are going to use the release version of CefSharp 3. This version can be found on NuGet:
https://github.com/cefsharp/CefSharp#nuget-packages

But it supports only x64 and x86 apllications. That means CefSharp 3 does not support the Any CPU setting or any other processor architecture. It is therefore, a good idea to copy the default solution settings from Any CPU into the x86 and x64 configurations and remove the Any CPU configuration (before referencing CefSharp 3):

  • Solution (Context Menu) > Configuration Manager
  • Click Active solution platform > New... to create the x86 and x64 settings:

    Click Active solution platform > Edit... to remove the Any CPU configuration (its not supported in CefSharp 3):

  • Be sure to also align the Project Settings with the Solution
  • Now lets add a reference to the solution via NuGet

    Click Solution (Context Menu) > Enable NuGet Package Restore
    Click Solution (Context Menu) > Manage NuGet Packages for Solution

    Add the CefSharp.WPF library into the solution

  • Save All edits and close Visual Studio. You really have to close it completely because the CefSharp reference will otherwise not show up correctly.
  • Re-open the CefSharp 3 solution and click build.
    Expectation: The project should build without an error and the References section in the sample project should show the CefSharp.WPF reference.
  • Lets open the MainWindow.xaml and lets add a CefSharp browser control into it:
<Window x:Class="Sample1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
Title="MainWindow" Height="550" Width="625">
<Grid>
<cefSharp:ChromiumWebBrowser Grid.Row="0"
Address="https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions" />
</Grid>
</Window>
  • That is, we have to add the CefSharp name space reference:
    xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
  • ...and the control itself:
    <cefSharp:ChromiumWebBrowser Grid.Row="0"<br>   Address="https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions" />
  • This code instantiates the ChromiomBrowser and points it at the Internet address indicated by the bindable Address property.
  • Now build and execute and enjoy. You should see the following window and be able to browse around in it. Be patience on starting it up though - this depends on you Internet speed and whether you use the debug version:

Congratulation :-) You just completed the first steps in series of articles towards building complex browsers into a MVVM compliant WPF architecture.

Using a Preview Version of CefSharp 3

There is also a preview version at MyGet.org for those who are running into problems using the current release version. You can use this preview version to check whether your problem has been reported before and may have already been fixed in the meantime. To do this, open:

  • Tools > NuGet Package Manager > Package Manager Settings
    to setup a new NuGet source: https://www.myget.org/F/cefsharp/

  • Go into the NuGet Package Manager and install the package from MyGet.org

    Now go back into the solution and add a reference to the MyGet package:

A Web-browser control can naturally be used to display content retrieved from a web server. But the strength of this control is not limited to displaying content from a web server. It is actually designed to retrieve and display content from virtually any local or remote source. One way of implementing this is shown in the next article of this series: http://www.codeproject.com/Articles/887148/Display-HTML-in-WPF-and-CefSharp-Tutorial-Part.

References

[1] Embedding Chrome in a WPF VB.NET Application using CEFSharp
     http://www.codeproject.com/Tips/648678/Embedding-Chrome-in-a-WPF-VB-NET-Application-using

[2] CefSharp repository on GitHub
     https://github.com/cefsharp/CefSharp
    
[3] CefSharp FAQ
     https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions
    
    CefSharp Wiki
    https://github.com/cefsharp/CefSharp/wiki

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Display HTML in WPF and CefSharp的更多相关文章

  1. WPF使用CefSharp嵌入网页

    1.点击项目应用下的管理NuGet程序包 2.在浏览中输入cefsharp-->查找 CefSharp.Wpf-->点击安装,等待安装完成 3.如果遇到一下问题将解决方案和项目都改成64位 ...

  2. WPF使用cefsharp

    最近在公司项目上会用到cefsharp.wpf,不知道为什么按照网上的配置一直无法运行成功,怎么配置可以参考以下这篇博文: http://www.cnblogs.com/TianFang/p/4573 ...

  3. WPF使用cefsharp 下载地址

    源码下载: https://github.com/cefsharp/CefSharp dll类库包下载nuget: https://www.nuget.org/packages/CefSharp.Wp ...

  4. WPF 开发 WebBrowser

    WebBrowser WebBrowser 报错如何屏蔽 CEF(Chromium Embedded Framework)       参考 WPF, Chrome Embedded and WebA ...

  5. 【WPF】使用CefSharp嵌入HTML网页

    需求:WPF项目中要做用户的商铺主页,由于考虑到每个商家的主页布局各不相同,不能用XAML写死布局.最好的办法是WPF这边XAML写好一个容器,用户使用HTML可视化编辑器(比如这个)来准备好网页,输 ...

  6. wpf中使用cefsharp加载本地html网页并实现cs和js的交互,并且cefsharp支持any cpu

    废话少说,直接上代码: 第一步: 第二步: 第三步: 第四步: App.xaml.cs对应的代码: using CefSharp; using CefSharp.Wpf; using System; ...

  7. wpf CefSharp 与 js交互

    原文:wpf CefSharp 与 js交互 通过 NuGet 获取 CefSharp.WpF 组件.  xmlns:cefSharp="clr-namespace:CefSharp.Wpf ...

  8. WPF中使用cefsharp

    原文:WPF中使用cefsharp 新入职一家公司,由写服务端接口变成了软硬件通信.服务器.客户端.C/S.B/S乱七八糟各种打杂.首先接收一个WPF项目,因为不熟WPF,再加上前端我也不熟,我打算使 ...

  9. CefSharp For WPF基本使用

    原文:CefSharp For WPF基本使用 Nuget引用 CefSharp.Wpf CefSharp.Common cef.redist.x64 cef.redist.x86 直接搜索安装Cef ...

随机推荐

  1. iOS 学习 - 12.NSMutableAttributedString 计算高度

    计算 NSMutableAttributedString 高度,必须要有两个属性 -(void)test{ UILabel *label = [[UILabel alloc]initWithFrame ...

  2. Java Security:公钥私钥、数字签名、消息摘要是什么

    1. 鲍勃有两把钥匙,一把是公钥,另一把是私钥. 2. 鲍勃把公钥送给他的朋友们----帕蒂.道格.苏珊----每人一把. 3. 苏珊要给鲍勃写一封保密的信.她写完后用鲍勃的公钥加密,就可以达到保密的 ...

  3. SQL 笔记

    --查询某一列在哪个表里 SELECT name , object_id , type , type_desc FROM sys.objects WHERE object_id IN ( SELECT ...

  4. CentOS6 Shell脚本/bin/bash^M: bad interpreter错误解决方法

    在windows下保存了一个脚本文件,用ssh上传到centos,添加权限执行nginx提示没有那个文件或目录.shell脚本放到/etc/init.d/目录下,再执行/etc/init.d/ngin ...

  5. oracle学习笔记系列------oracle 基本操作之基本函数的用法

    --创建一个accout账户表 CREATE TABLE account( id ) NOT NULL, recommender_id ), login_name ) NOT NULL, login_ ...

  6. linux清理内存命令

    1.清理前内存使用情况 free -m 2.开始清理  echo 1 > /proc/sys/vm/drop_caches3.清理后内存使用情况 free -m4.完成! 查看内存条数命令: # ...

  7. DNS 原理入门

    导读 DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识.本文详细介绍DNS的原理,以及如何运用工具软件观察它的运作.我的目标是,读完此文后,你就能完全理解DNS. 一 ...

  8. HTTP状态码分类说明

    状态码分类 HTTP状态码被分为五大类, 目前我们使用的HTTP协议版本是1.1, 支持以下的状态码.随着协议的发展,HTTP规范中会定义更多的状态码. 小技巧:  假如你看到一个状态码518, 你并 ...

  9. 用U盘制作启动盘后空间变小的恢复方法,清除U盘启动盘空间

    先把u盘插好,运行cmd, 输入diskpart,回车, (输入list disk,回车,能看到磁盘大致情况,u盘一般是磁盘1) 再输入select disk 1,回车, 再输入clean,回车, 关 ...

  10. HTTP常见状态码 200 301 302 404 500

    HTTP状态码(HTTP Status Code) 一些常见的状态码为: 一.1开头 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码.代码 说明 100 (继续) 请求者应当继续提出 ...