cefsharp.Wpf 入门

一、编译

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

1、nuget package:


  <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" />

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
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    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);
        }

        /// 
        /// 判断是否中文
        /// 
        /// 
        /// 
        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

你可能感兴趣的:(cefsharp)