zoukankan      html  css  js  c++  java
  • 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
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        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.offlineindex.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);
            }
            /// <summary>
            /// 判断是否中文
            /// </summary>
            /// <param name="Text"></param>
            /// <returns></returns>
            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
    {
        /// <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 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.
            }
        }
    }
  • 相关阅读:
    洛谷T44252 线索_分治线段树_思维题
    css 迷惑的position
    【二次元的CSS】—— 用 DIV + CSS3 画大白(详解步骤)
    直接使用sublime编译stylus
    w3schools网站的HTML教程之HTML编辑器
    【二次元的CSS】—— 纯CSS3做的能换挡的电扇
    《JavaScript Dom编程艺术》读书笔记(二)
    JQuery基础修炼-样式篇
    Vue.js 开发实践:实现精巧的无限加载与分页功能
    web前端教程《每日一题》(1-99)完结
  • 原文地址:https://www.cnblogs.com/lingLuoChengMi/p/9619298.html
Copyright © 2011-2022 走看看