【发布时间】:2020-04-06 13:57:57
【问题描述】:
我正在尝试使用 CefSharp.Wpf NuGet 包 (v79.1.360) 从磁盘显示一个简单的 html 网页。
页面依赖于与页面本身不同的目录中的本地文件(javascript、css 等...)。
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="file:///G:/AngularJS/Framework/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="MyApp.css">
<script src="file:///G:/AngularJS/Framework/angular.min.js"></script>
<script src="file:///G:/AngularJS/Framework/jquery.min.js"></script>
<script src="file:///G:/AngularJS/Framework/bootstrap.min.js"></script>
<script src="MyApp.js"></script>
<title></title>
</head>
<body>
<p class="font-weight-bold">Select address</p>
<table class="table table-hover">
<thead>
<tr class="bg-primary">
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Code</th>
<th scope="col">City</th>
<th scope="col">Street</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="address in Model.AddressList"
ng-click="Model.PickRow(address)"
ng-style="{'background-color': address.Background}">
<td>{{address.Id}}</td>
<td>{{address.Name}}</td>
<td>{{address.PostalCode}}</td>
<td>{{address.City}}</td>
<td>{{address.Street}}</td>
</tr>
</tbody>
</table>
</body>
</html>
我可以在基于 chromium 的网络浏览器 (Firefox) 上显示该页面,并修改设置: security.fileuri.strict_origin_policy false
-> Firefox 不是基于 chromium 的网络浏览器,所以我用 google chrome 对其进行了测试,它也能正常工作。
在我的网络研究中,我发现这篇文章与该问题相关(不幸的是,这篇文章来自 2014 年):
https://github.com/cefsharp/CefSharp/issues/572
于是我尝试为cefSharp控件设置浏览器设置:
<Window x:Class="WebControl.MyWebBrowser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cefSharpCore="clr-namespace:CefSharp;assembly=CefSharp.Core"
xmlns:cefSharpWpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
Height="300" Width="300">
<Grid>
<cefSharpWpf:ChromiumWebBrowser>
<cefSharpWpf:ChromiumWebBrowser.BrowserSettings>
<cefSharpCore:BrowserSettings WebSecurity="Disabled"
FileAccessFromFileUrls="Enabled"
UniversalAccessFromFileUrls="Enabled"/>
</cefSharpWpf:ChromiumWebBrowser.BrowserSettings>
</cefSharpWpf:ChromiumWebBrowser>
</Grid>
</Window>
但这不起作用。我也尝试在代码隐藏文件中设置设置,但没有成功。
长话短说:
Current result Expected result
如果有人有解决方案,请告诉我。
提前感谢您阅读这篇文章:-)
【问题讨论】:
-
Firefox 不是铬浏览器。您是否确认它可以在 Chrome 中运行?根据github.com/cefsharp/CefSharp/wiki/General-Usage#file-uri-file,我强烈劝阻任何人不要使用文件方案。推荐的方法是github.com/cefsharp/CefSharp/wiki/General-Usage#scheme-handler
-
是的,你是对的:Firefox 不是基于铬的,我的错 :-) 我用谷歌浏览器测试了它,它就像一个魅力。因为您不鼓励任何人使用文件方案,所以我将尝试使用方案处理程序的方式。感谢您的回复。
-
如果你可以移动你的文件,你可以使用cefsharp.github.io/api/79.1.x/html/…