C#: 如何将内部的 .html 文件资源加载到 WebBrowser 控件中?

6
我有一个名为test.html的文件,它是一个带有一些文本的基本html文件。 test.html是我的 c#项目中的资源,我有一个名为webbrowser1的 webbrowser需要加载我的 html文件。
那么如何将test.html加载到我的webbrowser中呢?
我尝试了这个方法,但它不起作用:
private void button1_Click(object sender, EventArgs e)
{
     webBrowser1.DocumentStream = 
         Properties.Resources.ResourceManager.GetStream("test.html");
}

有什么解决方案吗?

你有检查过这个吗?https://dev59.com/r2w05IYBdhLWcg3wgyHI - user1462616
我已经检查了所有这些,但是它们都不起作用...我的资源嵌入在解决方案中,而不是硬盘上!所以它不会像那样工作。 - Bitcoin Blogger
2个回答

10

我认为“test.html”不是一个有效的资源名称。 尝试使用“test_html”代替。 然后以下内容将正常工作。

我认为“test.html”不是有效的资源名称。建议改用“test_html”。这样就可以正常工作了。
private void button1_Click(object sender, EventArgs e)
{
     string html = Properties.Resources.test_html;
     webBrowser1.DocumentText = html;
}

所以如果HTML文件是这样的

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8" />
  <title></title>
</head>
<body>
  This is some resource HTML
</body>
</html>
你最终会得到: enter image description here

0
这是我对此问题的解决方案。我需要一种可重复使用的快速弹出式帮助方式,而不是创建多个外部HTML文件启动。
  1. 我将HTML文件添加到项目资源中。

    enter image description here

  2. 我将使用Capacities.html文件作为我的示例。

    <!DOCTYPE html>
    <html lang="en">
    
        <style>
    
        #mainHelp {
            width:500px;
            margin:0 auto;
    
        }
    
        </style>
    
        <div id="mainHelp">
            如果需要,可以为每种流体(水、油、燃料)和每个侧面(左侧、右侧)设置&quot流量容量&quot。如果容量设置为“ 0”,则禁用容量报告。容量是整数值,但可输入升或加仑。
    
        </div>
    </html>
    
  3. 在我的QuickHelpView.xaml.cs中,为了使窗口可重复使用,我发送一个调用者字符串以指示所需的资源。在这种情况下,需要"capacity"

    public void SetupQuickHelp(string _helpSelected)//, FileDataAccess aFilePathAccess
    {
        //filePathAccess = aFilePathAccess;
    
        string htmlText = "";
    
        if (_helpSelected == "instance")
        {
            htmlText = Properties.Resources.InstanceSettings;
        }
        else if (_helpSelected == "engine")
        {
            htmlText = Properties.Resources.EngineHours;
        }
        else if (_helpSelected == "capacity")
        {
            htmlText = Properties.Resources.Capacities;
        }
    
        webBrowserView.NavigateToString(htmlText);
    }
    
  4. 运行并选择帮助文件时,显示以下内容。

    enter image description here


感谢 @tdy 的编辑。这是我在stackoverflow上的第一篇帖子。 - Mike

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接