韦布罗瑟女士+;嵌入式HTML资源+;res://协议
我在VisualStudio项目中有一个嵌入式HTML资源(helloworld.htm)。(也就是说,我在项目中添加了一个HTML文件,并将其属性设置为“嵌入式资源” 在同一个应用程序中,我有一个WebBrowser控件 我想指示WebBrowser控件使用显示HTML资源 但是,我无法确定使用这种URL样式来寻址嵌入式资源所需的确切格式 有什么想法吗?谢谢韦布罗瑟女士+;嵌入式HTML资源+;res://协议,html,webbrowser-control,embedded-resource,Html,Webbrowser Control,Embedded Resource,我在VisualStudio项目中有一个嵌入式HTML资源(helloworld.htm)。(也就是说,我在项目中添加了一个HTML文件,并将其属性设置为“嵌入式资源” 在同一个应用程序中,我有一个WebBrowser控件 我想指示WebBrowser控件使用显示HTML资源 但是,我无法确定使用这种URL样式来寻址嵌入式资源所需的确切格式 有什么想法吗?谢谢 res://project.exe/helloworld.htm 其中: webBrowser1是WebBrowser控件 Reso
res://project.exe/helloworld.htm
其中:
是webBrowser1
控件WebBrowser
是您的exe/项目名称ResourceinWebBrowser
是嵌入的HTML资源的名称HTML
Stream docStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NameSpace.HTMLPage.html");
WebBrowser.DocumentStream = docStream;
这对我来说没有任何改动,而且非常简单。我希望它能让其他人受益!res:protocol没有死,它仍然是使用WebBrowser
控件将网页嵌入Windows应用程序的好方法。不幸的是,在我看来,exe和dll文件中有两种资源:C reso资源和.net资源。可以在.net dll中嵌入C资源,但我还没有弄清楚如何嵌入
为了回答您的问题,在中记录了res协议,但实际上构建dll或exe是一个棘手的部分。res协议非常简单。它的基本要点是指定res://,然后是可执行文件或dll的路径(如果在当前路径中,则只需dll名称)。对于HTML类型的资源,请使用文件名。下面是MSDN最近的一篇文章,讨论了有关res协议的一些已知问题:
构建dll或exe资源可能有点棘手。为了获得最简单的结果,请将所有资源设置为HTML类型(甚至是.js、.png、.jpg文件)。现代res文件允许您使用字符串命名文件,而不是使用定义的资源标识符命名资源。这样做将使您的生活更加轻松
高级提示:在资源名称中包含文件夹名称是一件棘手的事情;我还没有弄清楚。我认为您可以通过在资源名称中添加斜杠来模拟文件夹,但我认为res协议会被斜杠弄糊涂,认为路径的第一部分是资源类型。显式指定资源类型可能会缓解这种情况
高级技巧2:对于路径,IE的较新版本可以处理“\”字符,但如果需要指定dll或exe的绝对或相对位置,可以使用“%5C”替换“\”
其他资源:这是小助手类,以及如何调用它: 如何致电:
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));
}
// ******************************************************************
}
}
使用系统;
运用系统反思;
使用System.Windows;
使用System.Windows.Media.Imaging;
使用System.Windows.Resources;
命名空间HQ.Wpf.Util
{
公共类ResourceHelper
{
// ******************************************************************
///
///从定义为“资源”而不是“嵌入式资源”的嵌入式资源加载资源WPF位图图像(png、bmp,…)。
///
///无起始斜杠的路径
///通常是“Assembly.getExecutionGassembly()”。如果没有提及,我将使用调用程序集
///
公共静态BitmapImage LoadBitmapFromResource(字符串路径应用程序,程序集=null)
{
if(assembly==null)
{
assembly=assembly.GetCallingAssembly();
}
返回新的位图图像(ResourceHelper.GetLocationUri(pathInApplication,assembly));
}
// ******************************************************************
///
///资源应定义为“资源”,而不是“嵌入式资源”。
///
///路径以文件夹名(如果有)开始,然后是“/”,然后是。。。
///如果为null,则使用调用程序集查找资源
///
公共静态Uri GetLocationUri(字符串路径不带LeadingFlash,程序集=null)
{
if(路径不带引线间隙[0]=='/'))
{
PathWithLeadingslash=PathWithLeadingslash.Substring(1);
}
if(assembly==null)
{
assembly=assembly.GetCallingAssembly();
}
返回新Uri(@)pack://application:,,,/“+assembly.GetName().Name+“component/”+pathWithoutLeadingSlash,UriKind.Absolute);
}
// ******************************************************************
///
///资源应定义为“资源”,而不是“嵌入式资源”。
///例如:
///StreamResourceInfo=ResourceHelper.GetResourceStreamInfo(@“Resources/GraphicUserGuide.html”);
///如果(信息!=null)
/// {
///WebBrowser.NavigateToStream(信息流);
/// }
///
///路径以文件夹名(如果有)开始,然后是“/”,然后是。。。
///如果为null,则使用调用程序集查找资源
///
公共静态StreamResourceInfo GetResourceStreamInfo(字符串路径,程序集=null)
{
if(assembly==null)
{
assembly=assembly.GetCallingAssembly();
}
返回Application.GetResourceStream(ResourceHelper.GetLocationUri(路径,程序集));
}
// ******************************************************************
}
}
最简单的方法,可能不是s
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));
}
// ******************************************************************
}
}