zoukankan      html  css  js  c++  java
  • cefsharp.Wpf 入门

    一、编译

    版本 :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

    转自: https://blog.csdn.net/yeness/article/details/70141857
  • 相关阅读:
    如何改变拖动时鼠标悬浮样式
    Nginx的server为0.0.0.0/0.0.0.1的作用?
    redis的lua脚本拓展,返回nil及其判断
    lua异常捕获
    nginx配置及常见问题
    centos安装postgresql-10及操作
    23种设计模式
    php的function() use($args)用法
    lua中self.__index = self是什么意思?
    lor实践
  • 原文地址:https://www.cnblogs.com/javalinux/p/14283057.html
Copyright © 2011-2022 走看看