2015-06-16 16 views
5

Sto cercando di utilizzare il controllo del browser CefSharp nell'applicazione WinForms insieme al meccanismo LocalStorage.
Il problema è che il controllo del browser nell'applicazione cambia in LocalStorage non influisce su altre finestre del browser e non ottiene modifiche da altre finestre del browser Chrome.
L'HTML funziona all'interno del browser Chrome nativo e cambia localstorage e ottiene le notifiche delle modifiche.
Cosa mi manca?WinForms CefSharp Browser LocalStorage non funziona

C# Codice:

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> 

risposta

2

A meno che non si imposta un percorso per la posizione della cache in un oggetto CefSharp.CefSettings e passarlo al metodo Cef.Initialize(), ogni volta che la vostra applicazione viene avviato, i browser creeranno una nuova istanza cache, che verrà scartata all'uscita dalla tua app.

L'istanza di cache detiene anche i dati localStorage, così come i biscotti eventuali (si potrebbe desiderare di mantenere un utente firmato a?) e altre cose così.

Ho avuto lo stesso problema, ma ha trovato una soluzione qui: https://github.com/cefsharp/CefSharp/blob/v39.0.2/CefSharp.Example/CefExample.cs#L30-L32

La soluzione minima è semplicemente quello di aggiungere questo alla procedura di avvio dell'applicazione, come nel tuo Program.Main o nel tuo caso, il costruttore di classe di Form1:

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

CefSharp.Cef.Initialize(settings); 
Problemi correlati