简体   繁体   English

WinForms CefSharp 浏览器 LocalStorage 不工作

[英]WinForms CefSharp Browser LocalStorage not working

I'm trying to use CefSharp browser control at WinForms application along with the LocalStorage mechanism.我正在尝试在 WinForms 应用程序中使用 CefSharp 浏览器控件以及 LocalStorage 机制。
The problem is that the browser control in the application changes to LocalStorage don't affect other browser windows and it doesn't get changes from other chrome browser windows.问题是应用程序中的浏览器控件对 LocalStorage 的更改不会影响其他浏览器窗口,也不会从其他 chrome 浏览器窗口获得更改。
The HTML works inside native chrome browser and changes localstorage and get changes notifications. HTML 在本机 chrome 浏览器中工作并更改 localstorage 并获取更改通知。
What do I miss?我想念什么?

C# Code: C# 代码:

    public Form1()
    {
        InitializeComponent();

        CefSharp.Cef.Initialize();

        _browser = new ChromiumWebBrowser(URL_TO_LOAD);
        _browser.BrowserSettings = new CefSharp.BrowserSettings()
        {
            ApplicationCacheDisabled = false,
            FileAccessFromFileUrlsAllowed = true,
            JavascriptDisabled = false,
            LocalStorageDisabled = false,
            WebSecurityDisabled = true,
            JavaScriptOpenWindowsDisabled = false,
            JavascriptDomPasteDisabled = false
        };
        _browser.Load(URL_TO_LOAD);

        splitContainer1.Panel1.Controls.Add(_browser);
    }

HTML: HTML:

 <!DOCTYPE html>
 <html lang="en">
 <head>
      <meta charset="utf-8"/>
      <meta name="viewport" content="width=620"/>
      <title>HTML5 Demo: Storage Events</title>
 </head>
<body>
    <div>
        <p>
            <label for="data">Your test data:</label> <input type="text"
                name="data" value="" placeholder="change me" id="data" /> 
        </p>
        <p id="fromEvent">Waiting for data via<code>storage</code>event...
        </p>
    </div>
    <SCRIPT type="text/javascript">
    var addEvent = (function () {
          if (document.addEventListener) {
            return function (el, type, fn) {
              if (el && el.nodeName || el === window) {
                el.addEventListener(type, fn, false);
              } else if (el && el.length) {
                for (var i = 0; i < el.length; i++) {
                  addEvent(el[i], type, fn);
                }
              }
            };
          } else {
            return function (el, type, fn) {
              if (el && el.nodeName || el === window) {
                el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
              } else if (el && el.length) {
                for (var i = 0; i < el.length; i++) {
                  addEvent(el[i], type, fn);
                }
              }
            };
          }
        })();
    </SCRIPT>
    <script>
        alert("localStorage: " + localStorage);

        var dataInput = document.getElementById('data'), output = document
                .getElementById('fromEvent');
        addEvent(window, 'storage', function(event) {
            alert('change notification');
            if (event.key == 'storage-event-test') {
                output.innerHTML = event.newValue;
            }
        });
        addEvent(dataInput, 'keyup', function() {
            localStorage.setItem('storage-event-test', this.value);
        });

        var curStorageVal = localStorage.getItem('storage-event-test');
        if(curStorageVal != null && curStorageVal != '')
        {
            output.innerHTML = curStorageVal;
        }
    </script>


</body>
</html>  

Unless you set a path for the cache location in a CefSharp.CefSettings object and pass it to the Cef.Initialize() method, every time your app is started, the browser(s) will create a new cache instance, which will be discarded when your app exits.除非您在CefSharp.CefSettings对象中为缓存位置设置路径并将其传递给Cef.Initialize()方法, Cef.Initialize()每次您的应用程序启动时,浏览器都会创建一个新的缓存实例,该实例将被丢弃当您的应用退出时。

The cache instance also holds your localStorage data, as well as any cookies (you might want to keep a user signed in?) and other things as well.缓存实例还保存您的localStorage数据,以及任何 cookie (您可能希望让用户保持登录状态?)以及其他内容。

I had the same problem, but found a solution here: https://github.com/cefsharp/CefSharp/blob/v39.0.2/CefSharp.Example/CefExample.cs#L30-L32我遇到了同样的问题,但在这里找到了解决方案: https : //github.com/cefsharp/CefSharp/blob/v39.0.2/CefSharp.Example/CefExample.cs#L30-L32

One minimal solution is to add this to your application's startup procedure, like in your Program.Main or in your case, your Form1 class's constructor:一个最小的解决方案是将它添加到您的应用程序的启动过程中,例如在您的Program.Main或您的情况下,您的Form1类的构造函数:

var settings = new CefSharp.CefSettings
{
    CachePath = "cache"
};

CefSharp.Cef.Initialize(settings);

1.Get these from NuGet package manager : 1.从 NuGet 包管理器获取这些:
using CefSharp;使用 CefSharp; using CefSharp.WinForms;使用 CefSharp.WinForms;

2.Create instance for you CefSetting : 2.为你创建实例 CefSetting :
CefSettings settings = new CefSettings(); CefSettings settings = new CefSettings(); settings.CachePath = @"C:\\localstorage"; settings.CachePath = @"C:\\localstorage";

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM