2015-06-16 839 views
5

我想在WinForms應用程序中使用CefSharp瀏覽器控件以及LocalStorage機制。
問題是,應用程序中的瀏覽器控件更改爲LocalStorage不會影響其他瀏覽器窗口,並且不會從其他Chrome瀏覽器窗口獲取更改。
HTML在本地Chrome瀏覽器內部工作,並更改localstorage並獲取更改通知。
我錯過了什麼?WinForms CefSharp瀏覽器LocalStorage不工作

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:

<!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> 

回答

2

除非你在一個CefSharp.CefSettings對象設定的路徑的緩存位置,並把它傳遞給Cef.Initialize()方法,每次您的應用程序瀏覽器將創建一個新的緩存實例,當您的應用退出時,該緩存實例將被丟棄。

緩存實例還包含您的localStorage數據以及任何cookie (您可能希望保持用戶登錄?)以及其他內容。

我有同樣的問題,但在這裏找到了解決辦法:https://github.com/cefsharp/CefSharp/blob/v39.0.2/CefSharp.Example/CefExample.cs#L30-L32

最小的解決方案是簡單地把它添加到您的應用程序的啓動程序,就像你Program.Main或你的情況,你Form1類的構造函數:

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

CefSharp.Cef.Initialize(settings);