我的 Visual Studio 项目中有一个嵌入式 HTML 资源 (helloworld.htm)。(即,我在项目中添加了一个 HTML 文件并将其属性设置为“嵌入式资源”。
在同一个应用程序中,我有一个 WebBrowser 控件。
我想指示 WebBrowser 控件使用res:// 协议显示 HTML 资源。
但我无法弄清楚使用这种 URL 样式处理嵌入式资源所需的确切格式。
有任何想法吗?谢谢!
我的 Visual Studio 项目中有一个嵌入式 HTML 资源 (helloworld.htm)。(即,我在项目中添加了一个 HTML 文件并将其属性设置为“嵌入式资源”。
在同一个应用程序中,我有一个 WebBrowser 控件。
我想指示 WebBrowser 控件使用res:// 协议显示 HTML 资源。
但我无法弄清楚使用这种 URL 样式处理嵌入式资源所需的确切格式。
有任何想法吗?谢谢!
我知道这个线程已经死了,但我昨天不得不这样做并且无法让这些方法中的任何一个起作用。所以我做了一点研究,找到了下面的方法,使用 Stream 类。我想我会把它贴在这里,以防其他人遇到同样的废话:
Stream docStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NameSpace.HTMLPage.html");
WebBrowser.DocumentStream = docStream;
这对我有用,没有任何修补,而且非常简单。我希望它对其他人有益!
res:协议并没有失效,它仍然是使用控件将网页嵌入 Windows 应用程序的好方法WebBrowser
。 不幸的是,在我看来,exe 和 dll 文件中有两种类型的资源:C 资源和 .net 资源。可以将 C 资源嵌入到 .net dll 中,但我还没有弄清楚如何去做。
为了回答您的问题, res 协议记录在此处,但实际上构建 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可以处理'\'字符,但如果需要指定dll或exe的绝对或相对位置,可以使用'%5C'代替'\' .
附加资源:
MSDN 社交:Webbrowser 和 res:协议
DelphiDabbler:如何创建和使用 HTML 资源文件
res://project.exe/helloworld.htm
这是小助手类以及如何调用它:
如何调用:
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));
}
// ******************************************************************
}
}
webBrowser1.DocumentText = ResourceinWebBrowser.Properties.Resources.HTML.ToString();
在哪里:
webBrowser1
是WebBrowser
控制ResourceinWebBrowser
是您的 exe / 项目名称。HTML
是您嵌入的 html 资源的名称最简单的方法,也许不是最安全或最理智的方法,是拥有一个构成基本网页的 Settings 变量,在数据包中流式传输字符串时将您自己的标记标签放置在 REPLACE 中。这样,一旦网页的非动态部分完成后,您只需要将动态部分渲染为字符串中的 REPLACE 即可。然后设置 DoumentText = stringWebStream。请务必设置 AllowNavigation = True。
I know that it's been asked a long time ago, but here's how IE interprets the res:
protocol:
res://sFile[/sType]/sID
sFile Percent-encoded path and file name of the module that contains the resource.
sType Optional. String or numerical resource type. This can be either a custom resource or one of the predefined resource types that are recognized by the FindResource function. If a numerical resource type is specified, the number of the identifier must follow a # character. If this parameter is not specified, the default resource type is RT_HTML or RT_FILE.
sID String or numerical identifier of the resource. If a numerical identifier is specified, the actual number of the identifier, not the identifier itself, must follow a # character. See the example for more information.