基於CefSharp開發瀏覽器(一)項目搭建

1、建立項目

建立WPF (.Net Core)項目git

 2、CefSharp引用

程序包管理器控制檯引入CefSharpgithub

Install-Package CefSharp.Wpf -Version 85.3.130

 

CefSharp默認不支持AnyCPU,所以須要添加AnyCPU支持 https://github.com/cefsharp/CefSharp/issues/1714web

首先在Project中增長以下配置express

<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>

接着在App.xaml.cs中 增長AssemblyResolve事件動態解析加載失敗的程序集app

  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;
        }
    }

3、CefSharp初始化

在 InitializeCefSharp中添加代碼啓動DPI支持ui

Cef.EnableHighDPISupport();

禁用GPU及代理(啓用GPU可能會在網頁拖拽過程當中頁面閃爍)url

settings.CefCommandLineArgs.Add("disable-gpu","1");
settings.CefCommandLineArgs.Add("no-proxy-server","1");

4、引入ChromiumWebBrowser

新建用戶控件MWebBrowserUc並在Xaml中添加 ChromiumWebBrowser控件spa

<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方法3d

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);                
}

 運行以下

相關文章
相關標籤/搜索