C# WPF中嵌入Chrome浏览器

借鉴 

https://www.cnblogs.com/guxin/p/wpf-embed-html-by-cefsharp.html

 

搭建基本的开发环境

  新建WPF程序

  右击隐引用,选择管理NuGet包

  在nuget中搜索CefSharp,其中有CefSharp.WinForms和CefSharp.Wpf,在这里应该安装CefSharp.Wpf

  因为CefSharp不支持AnyCPU,需要VS中为项目编译平台单独指定x86和x64

  需重启vs才可以正常运行

XAML的简单示例

<Window x:Class="Demo.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:Demo"
        xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        xmlns:cef="clr-namespace:CefSharp;assembly=CefSharp.Core"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <wpf:ChromiumWebBrowser x:Name="Browser" Address="http://www.baidu.com"/>
    Grid>
Window>

  注意使用xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"和xmlns:cef="clr-namespace:CefSharp;assembly=CefSharp.Core"来引入浏览器控件所在的程序集。

 

直接在类中实现的简单示例

  借鉴  https://blog.csdn.net/yeness/article/details/70141857

using CefSharp;
using CefSharp.Wpf;
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;

namespace Demo1
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        ChromiumWebBrowser webView;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var setting = new CefSettings();
            if (Cef.IsInitialized == false)
            {
               // Cef.Initialize(setting, true, false);
                Cef.Initialize(setting);
            }
            webView = new ChromiumWebBrowser();
            grid.Children.Add(webView);

            /*string path= AppDomain.CurrentDomain.BaseDirectory + @"\gis.offline\index.html";
            webView.Address = path;*/
            webView.Address = "http://www.baidu.com";
           // webView.PreviewTextInput += new TextCompositionEventHandler(OnPreviewTextInput);
        }
        // 修复中文的bug
        protected void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char t in e.Text)
            {
                if (IsChinese(t))
                    webView.GetBrowser().GetHost().SendKeyEvent((int)CefSharp.Wpf.WM.CHAR, (int)t, 0);
            }
            base.OnPreviewTextInput(e);
        }
        /// 
        /// 判断是否中文
        /// 
        /// 
        /// 
        public bool IsChinese(char Text)
        {

            if ((int)Text > 127)
                return true;

            return false;
        }
    }
}

 新建类OpenPageSelf.cs,用于设置让新开的链接覆盖显示在本页上

  借鉴  https://www.cnblogs.com/wolf-sun/p/6929728.html

  新建OpenPageSelf类后,在webView.Address = "http://www.baidu.com";后添加webView.LifeSpanHandler = new OpenPageSelf();

using CefSharp;
using CefSharp.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo1
{
    /// 
    /// 在自己窗口打开链接
    /// 
    internal class OpenPageSelf:ILifeSpanHandler
    {
        public bool DoClose(IWebBrowser browserControl,IBrowser browser)
        {
            return false;
        }
        public void OnAfterCreated(IWebBrowser browserControl,IBrowser browser)
        {

        }

        public void OnBeforeClose(IWebBrowser chromiumWebBrowser, 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.
        }
    }
}

 

转载于:https://www.cnblogs.com/lingLuoChengMi/p/9619298.html

你可能感兴趣的:(C# WPF中嵌入Chrome浏览器)