【问题标题】:C#: How to load an internal .html file resource into a webbrowser control?C#:如何将内部 .html 文件资源加载到 webbrowser 控件中?
【发布时间】:2015-03-30 04:59:15
【问题描述】:

我得到了一个名为 test.html 的文件,它只是一个包含一些文本的基本 html 文件。 test.html 是我的 c# 项目中的一个资源,我有一个名为 webbrowser1 的 webbrowser,它需要加载我的 html 文件。

那么如何将 test.html 加载到我的浏览器中

我试过了,但它不起作用:

private void button1_Click(object sender, EventArgs e)
{
     webBrowser1.DocumentStream = 
         Properties.Resources.ResourceManager.GetStream("test.html");
}

请问有什么解决办法吗?

【问题讨论】:

标签: c# html resources embedded-resource


【解决方案1】:

这是我对这个问题的解决方案。除了制作几个外部 html 文件来启动之外,我还需要一种方法来制作可重复使用的快速弹出帮助。

  1. 我将 html 文件添加到项目资源中。

  2. 我将以Capacities.html 文件为例。

    <!DOCTYPE html>
    <html lang="en">
    
        <style>
    
        #mainHelp {
            width:500px;
            margin:0 auto;
    
        }
    
        </style>
    
        <div id="mainHelp">
            If desired, &quotFluid Capacities&quot can be set for each fluid (Water, Oil, Fuel) and side (Port, Stbd).
              If capacities are set to '0', this disables reporting of the capacities.  The capacities are whole number values,
               but can be entered as either liters or gallons.
    
        </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. 运行并选择帮助文件时,显示如下。

【讨论】:

  • 感谢@tdy 的编辑。这是我第一次在 stackoverflow 上发帖。
【解决方案2】:

我认为“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>

你最终会得到

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多