Jos*_*hwe 5 .net c# webview winforms
我需要显示一个本地 HTML 文件,其中包含通过外部或内部样式表包含的图像和字体。我想使用 .NET Framework 4.8 为 WinForms 引入的新 webView 控件。
我尝试了几种不同的方法。使用字体和图像托管 HTTP 服务器确实有效。遗憾的是,在推出该程序时,这不是解决方案,因为每台个人电脑的配置太多。
将图像和字体编码为 base64 字符串并将它们用作源代码确实有效。但我想使用 NavigateToLocalStreamUri 方法来获得一个易于阅读且不需要太多开销即可使其工作的解决方案。
NuGet 包 Microsoft.Toolkit.Forms.UI.Controls.WebView 安装在其最新的稳定版本 5.1.1 中
您可以在下方看到我当前用于尝试运行 NavigateToLocalStreamUri 的代码。
var uri = new Uri(someLocalPath);
webView1.NavigateToLocalStreamUri(uri, new CustomUriToStreamResolver());
Run Code Online (Sandbox Code Playgroud)
private class CustomUriToStreamResolver : IUriToStreamResolver
{
public Stream UriToStream(Uri uri)
{
var stream = new FileStream(uri.AbsolutePath, FileMode.Open);
return stream;
}
}
Run Code Online (Sandbox Code Playgroud)
预期的行为是打开位于的 HTML 文件someLocalPath
相反,它会引发以下异常,我不完全理解。
Could not find any resources appropriate for the specified culture or the
neutral culture. Make sure \"Microsoft.Toolkit.Win32.UI.Controls.DesignerUI.resources\"
was correctly embedded or linked into assembly
\"Microsoft.Toolkit.Forms.UI.Controls.WebView\" at compile time, or that all
the satellite assemblies required are loadable and fully signed.
Run Code Online (Sandbox Code Playgroud)
是我的问题CustomUriToStreamResolver
还是我不知道的潜在问题?
如果您知道someLocalPath
在 WinForm 中使用 webView 控件打开HTML 文件的任何其他方法,请告诉我。
小智 0
无需使用当前目录或获取程序集,只需使用 Application.ExecutablePath 属性:
//using System.IO;
string applicationDirectory = Path.GetDirectoryName(Application.ExecutablePath);
string myFile = Path.Combine(applicationDirectory, "Sample.html");
webMain.Url = new Uri("file:///" + myFile);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1923 次 |
最近记录: |