【问题标题】:MS WebBrowser + Embedded HTML Resource + res:// ProtocolMS WebBrowser + 嵌入式 HTML 资源 + res:// 协议
【发布时间】:2010-11-18 06:48:13
【问题描述】:

我的 Visual Studio 项目中有一个嵌入式 HTML 资源 (helloworld.htm)。 (即,我在项目中添加了一个 HTML 文件并将其属性设置为“嵌入式资源”。

在同一个应用程序中,我有一个 WebBrowser 控件。

我想使用 res:// protocol 指示 WebBrowser 控件显示 HTML 资源。

但我无法确定使用这种 URL 样式处理嵌入式资源所需的确切格式。

有什么想法吗?谢谢!

【问题讨论】:

  • 我之前已经让它工作了,但是忘记了带有 res:// 协议的 URL 的格式。
  • 有什么更新吗?

标签: html webbrowser-control embedded-resource


【解决方案1】:
res://project.exe/helloworld.htm

【讨论】:

    【解决方案2】:
    webBrowser1.DocumentText = ResourceinWebBrowser.Properties.Resources.HTML.ToString();
    

    地点:

    • webBrowser1WebBrowser 控件
    • ResourceinWebBrowser 是您的 exe / 项目名称。
    • HTML 是您嵌入的 html 资源的名称

    【讨论】:

      【解决方案3】:

      我知道这个线程已经死了,但我昨天不得不这样做,并且无法让这些方法中的任何一个起作用。所以我做了一些研究,找到了下面的方法,使用 Stream 类。我想我会把它贴在这里以防万一其他人遇到同样的废话:

      Stream docStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NameSpace.HTMLPage.html");
      WebBrowser.DocumentStream = docStream;
      

      这对我有用,没有任何修补,而且非常简单。我希望它对其他人有所帮助!

      【讨论】:

      【解决方案4】:

      res: 协议并没有失效,它仍然是使用WebBrowser 控件将网页嵌入 Windows 应用程序的好方法。 不幸的是, 在我看来,exe 和 dll 文件中有两种类型的资源:C 资源和 .net 资源。可以将 C 资源嵌入到 .net dll 中,但我还没有弄清楚如何去做。

      为了回答您的问题, res 协议记录在 here,但实际上构建 dll 或 exe 是棘手的部分。 res 协议很简单。它的基本要点是您指定 res://,然后是可执行文件或 dll 的路径(如果它在当前路径中,则只是 dll 名称)。对于 HTML 类型的资源,请在后面加上文件名。这是最近的 MSDN 文章,讨论了 res 协议的一些已知问题:http://support.microsoft.com/kb/220830

      构建 dll 或 exe 资源可能有点棘手。为获得最简单的结果,请将所有资源设为 HTML 类型(甚至是 .js、.png、.jpg 文件)。现代 res 文件允许您使用字符串命名文件,而不是使用 #defined 资源标识符来命名您的资源。这样做会让你的生活轻松很多。

      高级提示:在资源名称中包含文件夹名称很棘手;我还没想好我们的。我认为您可以通过在资源名称中添加斜杠来模拟文件夹,但我认为 res 协议会被斜杠混淆,认为路径的第一部分是资源类型。显式指定资源类型可以缓解这种情况。

      进阶技巧2:对于路径,较新版本的IE可以处理'\'字符,但如果需要指定,可以使用'%5C'代替'\' dll 或 exe 的绝对或相对位置。

      其他资源:
      MSDN Social: Webbrowser and res: protocol
      DelphiDabbler: How to create and use HTML resource files

      【讨论】:

      • 这是最好的答案,现在我知道 res 协议的限制,因为我无法以编程方式为 .mht 文件设置 mime 类型
      【解决方案5】:

      这是小助手类以及如何调用它:

      如何调用:

      StreamResourceInfo info = 
          ResourceHelper.GetResourceStreamInfo(@"Resources/GraphicUserGuide.html");
      if (info != null)
      {
          WebBrowser.NavigateToStream(info.Stream);
      }
      

      助手类:

      using System;
      using System.Reflection;
      using System.Windows;
      using System.Windows.Media.Imaging;
      using System.Windows.Resources;
      
      namespace HQ.Wpf.Util
      {
          public class ResourceHelper
          {
              // ******************************************************************
              /// <summary>
              /// Load a resource WPF-BitmapImage (png, bmp, ...) from embedded resource defined as 'Resource' not as 'Embedded resource'.
              /// </summary>
              /// <param name="pathInApplication">Path without starting slash</param>
              /// <param name="assembly">Usually 'Assembly.GetExecutingAssembly()'. If not mentionned, I will use the calling assembly</param>
              /// <returns></returns>
              public static BitmapImage LoadBitmapFromResource(string pathInApplication, Assembly assembly = null)
              {
                  if (assembly == null)
                  {
                      assembly = Assembly.GetCallingAssembly();
                  }
      
                  return new BitmapImage(ResourceHelper.GetLocationUri(pathInApplication, assembly));
              }
      
              // ******************************************************************
              /// <summary>
              /// The resource should be defined as 'Resource' not as 'Embedded resource'.
              /// </summary>
              /// <param name="pathWithoutLeadingSlash">The path start with folder name (if any) then '/', then ...</param>
              /// <param name="assembly">If null, then use calling assembly to find the resource</param>
              /// <returns></returns>
              public static Uri GetLocationUri(string pathWithoutLeadingSlash, Assembly assembly = null)
              {
                  if (pathWithoutLeadingSlash[0] == '/')
                  {
                      pathWithoutLeadingSlash = pathWithoutLeadingSlash.Substring(1);
                  }
      
                  if (assembly == null)
                  {
                      assembly = Assembly.GetCallingAssembly();
                  }
      
                  return new Uri(@"pack://application:,,,/" + assembly.GetName().Name + ";component/" + pathWithoutLeadingSlash, UriKind.Absolute);
              }
      
              // ******************************************************************
              /// <summary>
              /// The resource should be defined as 'Resource' not as 'Embedded resource'.
              /// Example:            
              ///     StreamResourceInfo info = ResourceHelper.GetResourceStreamInfo(@"Resources/GraphicUserGuide.html");
              ///     if (info != null)
              ///     {
              ///         WebBrowser.NavigateToStream(info.Stream);
              ///     }
              /// </summary>
              /// <param name="path">The path start with folder name (if any) then '/', then ...</param>
              /// <param name="assembly">If null, then use calling assembly to find the resource</param>
              /// <returns></returns>
              public static StreamResourceInfo GetResourceStreamInfo(string path, Assembly assembly = null)
              {
                  if (assembly == null)
                  {
                      assembly = Assembly.GetCallingAssembly();
                  }
      
                  return Application.GetResourceStream(ResourceHelper.GetLocationUri(path, assembly));
              }
      
              // ******************************************************************
      
          }
      }
      

      【讨论】:

        【解决方案6】:

        最简单的方法,也许不是最安全或最理智的方法,是拥有一个构成基本网页的 Settings 变量,在数据包中流式传输字符串时将您自己的标记标签放置到 REPLACE。这样,一旦网页的非动态部分完成后,您只需要将动态部分渲染为字符串中的 REPLACE 即可。然后设置 DoumentText = stringWebStream。请务必设置 AllowNavigation = True。

        【讨论】:

          【解决方案7】:

          我知道很久以前有人问过,但是here's how IE 解释了res: 协议:

          res://sFile[/sType]/sID

          sFile 包含资源的模块的百分比编码路径和文件名。

          sType 可选。字符串或数字资源类型。这可以是自定义资源或预定义的资源类型之一 被FindResource 函数识别。如果一个数字资源 指定类型,标识符的编号必须跟在#之后 特点。如果不指定此参数,则默认资源 类型为 RT_HTML 或 RT_FILE。

          sID 资源的字符串或数字标识符。如果指定了数字标识符,则为标识符的实际编号,而不是 标识符本身,必须跟在 # 字符之后。有关更多信息,请参见示例 信息。

          【讨论】:

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