我正在尝试在WinForms应用程序中使用CefSharp浏览器控件以及LocalStorage机制。
问题是应用程序中的浏览器控件更改为LocalStorage不会影响其他浏览器窗口,并且它不会从其他Chrome浏览器窗口获得更改。
HTML在本机chrome浏览器中工作,并更改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>
答案 0 :(得分:3)
除非您在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);
答案 1 :(得分:-1)
1。从NuGet程序包管理器中获取它们:
使用CefSharp;
使用CefSharp.WinForms;
2。为您创建实例CefSetting:
CefSettings设置=新的CefSettings();
settings.CachePath = @“ C:\ localstorage”;