9

Visual Studioプロジェクト内に埋め込みHTMLリソース(helloworld.htm)があります。(つまり、プロジェクトにHTMLファイルを追加し、そのプロパティを「埋め込みリソース」に設定しました。

同じアプリケーション内に、WebBrowserコントロールがあります。

res://プロトコルを使用してHTMLリソースを表示するようにWebBrowserコントロールに指示したいと思います。

しかし、このスタイルのURLを使用して埋め込みリソースをアドレス指定するために必要な正確な形式を理解することはできません。

何か案は?ありがとう!

4

7 に答える 7

12

私はこのスレッドが死んでいることを知っていますが、私は昨日これをしなければならず、これらのメソッドのいずれも機能させることができませんでした. そこで、少し調べてみたところ、Stream クラスを使用して以下の方法を見つけました。他の誰かが同じナンセンスに遭遇した場合に備えて、ここに投稿すると思いました。

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

これは、いじくり回すことなく私にとってはうまくいき、とても簡単でした. それが他の誰かに利益をもたらすことを願っています!

于 2011-02-03T13:38:18.600 に答える
7

res:プロトコルは死んでおらず、コントロールを使用して Web ページを Windows アプリケーションに埋め込む優れた方法ですWebBrowser残念ながら、 exe ファイルと dll ファイルには、C リソースと .net リソースの 2 種類のリソースがあるようです。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 ファイルも含めて)。#defined リソース識別子でリソースに名前を付ける代わりに、最新の res ファイルを使用すると、文字列でファイルに名前を付けることができます。これを行うと、あなたの人生はずっと楽になります。

高度なヒント:リソース名にフォルダー名を含めるのは注意が必要です。私はまだそれを理解していません。リソース名にスラッシュを入れることでフォルダーをシミュレートできると思いますが、パスの最初の部分がリソースの種類であると考えて、スラッシュによって res プロトコルが混乱すると思います。リソースタイプを明示的に指定すると、これが軽減される場合があります。

高度なヒント 2: IE の新しいバージョンのパスは '\' 文字を処理できますが、dll または exe の絶対または相対位置を指定する必要がある場合は、'\' の代わりに '%5C' を使用できます。 .

追加リソース:
MSDN ソーシャル: Web ブラウザーと res: プロトコル
DelphiDabbler: HTML リソース ファイルの作成方法と使用方法

于 2013-03-28T01:03:08.783 に答える
2
res://project.exe/helloworld.htm
于 2009-08-17T20:48:08.617 に答える
1

これは小さなヘルパー クラスとその呼び出し方です。

呼び出し方:

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));
        }

        // ******************************************************************

    }
}
于 2014-01-23T21:49:55.023 に答える
0

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.

于 2015-03-23T02:16:17.143 に答える
0
webBrowser1.DocumentText = ResourceinWebBrowser.Properties.Resources.HTML.ToString();

どこ:

  • webBrowser1WebBrowserコントロールです
  • ResourceinWebBrowserあなたのexe /プロジェクト名です。
  • HTML埋め込み html リソースの名前です
于 2009-12-14T16:19:27.447 に答える