gpt4 book ai didi

c# - 使用.NET Framework 4.8 WinForm webView控件显示本地html

转载 作者:行者123 更新时间:2023-11-30 12:54:26 25 4
gpt4 key购买 nike

我需要显示一个本地 HTML 文件,其中包含通过外部或内部样式表包含的图像和字体。我想使用 .NET Framework 4.8 for 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());
private class CustomUriToStreamResolver : IUriToStreamResolver
{
public Stream UriToStream(Uri uri)
{
var stream = new FileStream(uri.AbsolutePath, FileMode.Open);
return stream;
}
}

预期的行为是打开位于 someLocalPath 的 HTML 文件相反,它抛出以下异常,我不完全理解。

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.

问题是我的 CustomUriToStreamResolver 还是存在我不知道的潜在问题?

如果您知道在 WinForm 中使用 webView 控件打开 someLocalPath HTML 文件的任何其他方法,请告诉我。

最佳答案

不使用当前目录或获取程序集,而是使用 Application.ExecutablePath 属性:

//using System.IO;  

string applicationDirectory = Path.GetDirectoryName(Application.ExecutablePath);

string myFile = Path.Combine(applicationDirectory, "Sample.html");

webMain.Url = new Uri("file:///" + myFile);

关于c# - 使用.NET Framework 4.8 WinForm webView控件显示本地html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56473108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com