CefSharp ChromiumWebBrowser无需网络连接即可从缓存加载

时间:2019-01-21 22:15:49

标签: c# .net winforms cefsharp chromium-embedded

我创建了一个.net应用程序,该应用程序必须能够在没有Internet连接的情况下使用。此应用程序使用CefSharp C#库将我的rails应用程序嵌入Windows窗体中。下面是我的Main()函数,用于设置cefsharp设置

static void Main()
        {
            //For Windows 7 and above, best to include relevant app.manifest entries as well
            Cef.EnableHighDPISupport();

            //We're going to manually call Cef.Shutdown below, this maybe required in some complex scenarios
            CefSharpSettings.ShutdownOnExit = false;

            var cachePath = "cache";
            if (!Directory.Exists(cachePath)) Directory.CreateDirectory(cachePath);



            var settings = new CefSettings();
            settings.CachePath = cachePath;
            settings.LogFile = "f7chromium.log";

            settings.BrowserSubprocessPath = @"x86\CefSharp.BrowserSubprocess.exe";

            //Perform dependency check to make sure all relevant resources are in our output directory.
            Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
            Debug.WriteLine("Starting");

            host = MyServer.Run();

            //var browser = new BrowserForm();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());




            Console.ReadLine();
        }

用于初始化表格的代码

public partial class Form1 : Form
    {
        public ChromiumWebBrowser chromeBrowser;

        private static string baseUrl = "https://goo.com/";

        public void InitializeChromium()
        {


            var url = baseUrl;

            chromeBrowser = new ChromiumWebBrowser(url);

            // Add it to the form and fill it to the form window.
            this.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
        }

        public Form1()
        {
            FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
            Bounds = Screen.PrimaryScreen.Bounds;

            InitializeComponent();
            InitializeChromium();

            HotkeyManager.Current.AddOrReplace("Close", Keys.F10, OnClose);
            HotkeyManager.Current.AddOrReplace("Reset", Keys.F8, OnReset);
            HotkeyManager.Current.AddOrReplace("Refresh", Keys.F5, OnRefresh);
            HotkeyManager.Current.AddOrReplace("DevTools", Keys.F3, DevTools);
        }

        private void DevTools(object sender, HotkeyEventArgs e)
        {
            chromeBrowser.ShowDevTools();
        }

        private void OnReset(object sender, HotkeyEventArgs e)
        {
            var result = MessageBox.Show("Confirm reset all station data", "Confirm Reset", MessageBoxButtons.OKCancel);

            if (result == DialogResult.OK) chromeBrowser.Load(baseUrl + "/reset");
        }

        private void OnRefresh(object sender, HotkeyEventArgs e)
        {
            chromeBrowser.Reload();
        }

        private void OnClose(object sender, HotkeyEventArgs e)
        {
            var result = MessageBox.Show("Close application?", "Confirm Exit", MessageBoxButtons.OKCancel);

            if (result == DialogResult.OK)
            {
                Process.Start(@"C:\");
                Process.Start(@"powershell.exe");
                Close();
            }               
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
             Cef.Shutdown();
        }

    }

是否可以像不启用Internet那样启动应用程序,就像通​​过启用show-page-copy按钮从缓存中加载chrome中的网站一样?

0 个答案:

没有答案