基于CefSharp开发(一)开发什么?没想好
作者:互联网
一、创建项目
创建WPF (.Net Core)项目
二、CefSharp引用
程序包管理器控制台引入CefSharp
Install-Package CefSharp.Wpf -Version 85.3.130
CefSharp默认不支持AnyCPU,因此需要添加AnyCPU支持 https://github.com/cefsharp/CefSharp/issues/1714
首先在Project中增加如下配置
<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>
接着在App.xaml.cs中 增加AssemblyResolve事件动态解析加载失败的程序集
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; } }
三、CefSharp初始化
在 InitializeCefSharp中添加代码启动DPI支持
Cef.EnableHighDPISupport();
禁用GPU及代理(启用GPU可能会在网页拖拽过程中页面闪烁)
settings.CefCommandLineArgs.Add("disable-gpu","1"); settings.CefCommandLineArgs.Add("no-proxy-server","1");
四、引入ChromiumWebBrowser
新建用户控件MWebBrowserUc并在Xaml中添加 ChromiumWebBrowser控件
<UserControl x:Class="MWebBrowser.MWebBrowserUc" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:web="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <web:ChromiumWebBrowser x:Name="CefWebBrowser"/> </Grid> </UserControl>
cs代码中增加Load方法
public void Load(string url) {
CefWebBrowser.Load(url); }
在MainWindow中引用该UserControl
<Window x:Class="MWebBrowser.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:webbrowser="clr-namespace:MWebBrowser" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <webbrowser:MWebBrowserUc x:Name="MWebBrowser"/> </Grid> </Window>
并在MainWindow Load事件中执行
private void MainWindow_Loaded(object sender, RoutedEventArgs e) { string url = "http://www.baidu.com"; MWebBrowser.Load(url); }
运行如下
标签:Load,CefSharp,string,settings,url,没想,开发,CurrentDomain 来源: https://www.cnblogs.com/mchao/p/13914726.html