zoukankan      html  css  js  c++  java
  • WPF中使用CefSharp遇到的一个问题记录

      因为最近项目中要嵌入几个合作方的web页面,原先是准备使用miniblink(一个国内大神的开源项目)的.NET封装的,不过试用后界面渲染效果一般并且封装是以UI显示为目的的,如果你点击了网页中某个a标签它不能开新窗口显示,而是直接跳转,这个需求差异需要自己完成,使用不变,故选择CefSharp来做。

      使用CefSharp的过程简单而顺利,直接新建项目并nuget引用即可,GitHub上的wiki里还有中文说明,在将它集成到项目中时候发现每次加载web页面时从ChromiumWebBrowser控件开始显示到DOM加载完毕这段时间内控件的背景色是白色的,但我界面风格是深色的显得很突兀,于是走了一波谷歌到处查找解决办法,大部分结果是说直接在Cef初始化时候添加背景色,代码如下:

     1      private static void InitializeCefSharp()
     2         {
     3             var settings = new CefSettings();
     4             settings.Locale = "zh-CN";
     5             //日志等级
     6             settings.LogSeverity = LogSeverity.Warning;
     7             settings.LogFile = "Logs";
     8 
     9             settings.BackgroundColor = ((uint)255 << 24) | ((uint)22 << 16) | ((uint)28 << 8) | ((uint)43 << 0);// Color.FromRgb(22, 28, 43); ;//0xFF161C2B;
    10             settings.WindowlessRenderingEnabled = true;
    11             //settings.CefCommandLineArgs.Add("background-color = red");
    12 
    13             // Set BrowserSubProcessPath based on app bitness at runtime
    14             settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
    15                                                    Environment.Is64BitProcess ? "x64" : "x86",
    16                                                    "CefSharp.BrowserSubprocess.exe");
    17 
    18             // Make sure you set performDependencyCheck false
    19             Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
    20         }

      但是测试过程中发现无论设置BackgroundColor还是用CefCommandLineArgs.Add("background-color = red")来做都是不行的,依旧是有个白色背景过渡,抓狂...

      后来无意中翻到StackOverFlow上有人谈论过这个情况,回答问题者用此办法可实现,于是我照着在使用了ChromiumWebBrowser的View的Code-Behind构造函数中加了如下代码:

            public DeputyPositionView()
            {
                InitializeComponent();
    
                var browserSettings = new CefSharp.BrowserSettings();
                browserSettings.BackgroundColor = 0xFF161C2B;
                webView1.BrowserSettings = browserSettings;
            }    

      再将项目跑起来白色背景果然不出现了,取而代之的颜色是我这里设置的0xFF161C2B

      按照常理来说初始化时候解决不了的问题那肯定会想到到控件自己的属性里去想办法解决,之所以这个问题浪费了我上午大部分时光就是因为CefSharp自己的api文档里对这个BackgroundColor属性的说明有问题(至少我表面操作是这样的,具体我没有去深究),我原地打转各种值和属性变换着试折腾好久,现贴上这段说明(原文连接):

      Opaque background color used for the browser before a document is loaded and when no document color is specified. By default the background color will be the same as CefSettings.background_color. Only the RGB compontents of the specified value will be used. The alpha component must greater than 0 to enable use of the background color but will be otherwise ignored.

      其中说这个IBrowserSettings接口中BackgroundColor Property默认和CefSettings的background_color是一样的,而我前面InitializeCefSharp函数里的settings.BackgroundColor就是它!这个接口说明明显与我实际操作结果不一样,这个情况不知道是否有读者遇到过,如果是我使用不当导致这个情况请在留言区告诉我。

  • 相关阅读:
    12、SpringBoot-CRUD增加数据
    12、SpringBoot-CRUD增加数据
    Cache操作类
    pythonhttp
    python学习
    自动化测试LoadRunner
    pythonweb自动化测试
    python学习工具篇
    python学习
    自动化测试之python安装
  • 原文地址:https://www.cnblogs.com/thsgar/p/13577922.html
Copyright © 2011-2022 走看看