zoukankan      html  css  js  c++  java
  • C# CefSharp如何在Winforms应用程序中使用

    最近做了一个很小的功能,在网页上面打开应用程序,用vs的debug调试,可以正常打开应用程序,可布置到iis上面却无法运行应用程序,吾百度之,说是iis权限问题,吾依理做之,可怎么折腾也不行。最后boss给了两种方案,第一,弃b/s改c/s,第二,用CefSharp把b/s网站嵌进去。b/s网站已做完,弃之可惜,吾便用了CefSharp。

    以下是使用CefSharp的步骤:

     1.创建一个基本的Winforms应用程序,并添加CefSharp使用NuGet包。

        在创建之前,请确保计算机已安装:CefSharp 45.0及更高版本需要安装VC 2013 Redistributable Package x86,早期版本需要VC 2012 Redistributable Package x86。如果未安装,会报以下错误。

      An unhandled exception of type 'System.IO.FileNotFoundException' occurred in browser.exe Additional information: Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies.

        通常安装最新版本的CefSharp,建议完全关闭VS,然后重新打开(这样可以确保您的引用显示,并有完整的intellisense),否则你可能会发生错误:找不到类型或命名空间名称“Cefsharp”(是否缺少using指令或程序集引用?)

    2 更改平台配置(x86,x64或AnyCPU)

       吾用的CefSharp版本是51以上,所以要修改配置:

       首先,搜索your-project-name.csproj文件,并在第一个 <PropertyGroup>的节点添加:

         <CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>

       然后修改app.config文件:

         <runtime>

        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath="x86"/>
        </assemblyBinding>
     </runtime>
    3 cefsharp已经安装并配置完成,现在就可以写代码了.
      这是winfrom的代码
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using CefSharp;
    using CefSharp.WinForms;
    using System.Configuration;
    
    namespace VR.Winfrom
    {
        public partial class Form1 : Form
        {
            public ChromiumWebBrowser chromeBrowser;
    
            public Form1()
            {
                InitializeComponent();
                this.WindowState = FormWindowState.Maximized;
                InitializeChromium();
                // Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3
                chromeBrowser.RegisterJsObject("formProcess", new FormProcess(chromeBrowser, this));
            }
    
            public void InitializeChromium()
            {
                CefSettings settings = new CefSettings();
                // Initialize cef with the provided settings
                Cef.Initialize(settings);
                // Create a browser component
                string url = ConfigurationManager.AppSettings["Url"];
                chromeBrowser = new ChromiumWebBrowser(url);
    
                // Add it to the form and fill it to the form window.
                this.Controls.Add(chromeBrowser);
                chromeBrowser.Dock = DockStyle.Fill;
    
                // Allow the use of local resources in the browser
                BrowserSettings browserSettings = new BrowserSettings();
                browserSettings.FileAccessFromFileUrls = CefState.Enabled;
                browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
                browserSettings.WebSecurity = CefState.Enabled;
                chromeBrowser.BrowserSettings = browserSettings;
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                Cef.Shutdown();
            }
        }
    }
    这是执行应用程序的代码
    using System.Diagnostics;
    using CefSharp.WinForms;
    using VR.DAL;
    using System;

    namespace VR.Winfrom
    {
       public class FormProcess
        {
            // Declare a local instance of chromium and the main form in order to execute things from here in the main thread
            private static ChromiumWebBrowser _instanceBrowser = null;
            // The form class needs to be changed according to yours
            private static Form1 _instanceMainForm = null;
            
            public FormProcess(ChromiumWebBrowser originalBrowser, Form1 mainForm)
            {
                _instanceBrowser = originalBrowser;
                _instanceMainForm = mainForm;
            }

            public void opencmd(string filePath)
            {
                  
                    string file = @"" + filePath;
                    ProcessStartInfo start = new ProcessStartInfo(file);
                    Process.Start(start);
            }

           
        }
    }

    最后web页面的调用

    <button class="btn btn-primary" onclick="FormProcess.opencmd('C://Program Files (x86)//Google//Chrome//Application//chrome.exe');">Open</button>

     参考网址:http://www.libs.org.cn/index.php?m=content&c=index&a=show&catid=90&id=129

     
      
     
  • 相关阅读:
    js截取字符串区分汉字字母代码
    List 去处自定义重复对象方法
    63. Unique Paths II
    62. Unique Paths
    388. Longest Absolute File Path
    41. First Missing Positive
    140. Word Break II
    139. Word Break
    239. Sliding Window Maximum
    5. Longest Palindromic Substring
  • 原文地址:https://www.cnblogs.com/shimiyan/p/6932073.html
Copyright © 2011-2022 走看看