cefsharp.Wpf 入门

本文介绍如何使用CefSharp.WPF 49.0.1版本创建浏览器控件,并解决中文输入问题,同时演示了C#与JavaScript间的交互调用方法及解决界面闪烁的技术方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、编译

版本 :CefSharp.Wpf49.0.1
开发工具 :vs2012

1、nuget package:

<packages>
  <package id="cef.redist.x64" version="3.2623.1401" targetFramework="net40" />
  <package id="cef.redist.x86" version="3.2623.1401" targetFramework="net40" />
  <package id="CefSharp.Common" version="49.0.1" targetFramework="net40" />
  <package id="CefSharp.Wpf" version="49.0.1" targetFramework="net40" />
</packages>

2、vs编译
编译前 右键解决方案 ->属性 -> 配置属性 ->配置 改为 “x86” / “x64” ,开始编译

3、“CefSharp.Core.dll”不是有效 x64 /32位 有效dll
去到微软官方地址下载相应的vc++2012/2013
2012地址:https://www.microsoft.com/zh-CN/download/details.aspx?id=30679
2013地址:https://www.microsoft.com/zh-CN/download/details.aspx?id=40784
安装后,重新编译

二、使用


using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        CefSharp.Wpf.ChromiumWebBrowser webView;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void browserGrid_Loaded(object sender, RoutedEventArgs e)
        {
            var setting = new CefSharp.CefSettings();
            if (CefSharp.Cef.IsInitialized == false)
                CefSharp.Cef.Initialize(setting, true, false);

            webView = new CefSharp.Wpf.ChromiumWebBrowser();
            this.browserGrid.Children.Add(webView);

            string path = AppDomain.CurrentDomain.BaseDirectory + @"\gis.offline\index.html";
            webView.Address = path; //"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;
        }

    }
}

上含解决中文输入法的简单处理。

三、c#与js互相调用

1、js调用c#

 private void browserGrid_Loaded(object sender, RoutedEventArgs e)
 {
           ...
            webView.RegisterJsObject("c_Obj", new MainWindow(), false);
            this.browserGrid.Children.Add(webView);
            ...
 }
........
    <script type="text/javascript">
            c_Obj.ShowTest("hello world");  
    </script >
 </body>

2、c#调用js

<Grid Margin="0,0,0,0" Grid.Row="0">
      <Button Click="Button_Click" Content="click me to use js" />
</Grid>
       private void Button_Click(object sender, RoutedEventArgs e)
        {
            webView.GetBrowser().MainFrame.ExecuteJavaScriptAsync("fun()");
        }
    <script type="text/javascript">
            c_Obj.ShowTest("hello world");

            function fun()
            {
                alert("js hello world");
            }
    </script >
 </body>

四、cefsharp解决闪烁

var setting = new CefSharp.CefSettings();
setting.CefCommandLineArgs.Add("disable-gpu", "1"); // 禁用gpu
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值