访问 C# WPF 应用程序中的资源(同一个程序集)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18257250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:27:01  来源:igfitidea点击:

Accessing Resources in a C# WPF Application (Same Assembly)

c#wpfxamlvisual-studio-2012

提问by Literata

(Before I jump into the nitty-gritty, I want to set the context: I'm trying to load a WPF frame with the contents of an .html file that I'm including in my project as a resource.)

(在我进入细节之前,我想设置上下文:我正在尝试加载一个 WPF 框架,其中包含我作为资源包含在我的项目中的 .html 文件的内容。)

I create a new WPF Application; I add a new folder called 'foofiles' to the project, and I add a couple of files (page1.foo and page2.foo) to that folder.

我创建了一个新的 WPF 应用程序;我向项目添加了一个名为“foofiles”的新文件夹,并向该文件夹添加了几个文件(page1.foo 和 page2.foo)。

For each newly added .foo file, I right-click on it, go to "Properties," and set the Build Action to 'Resource,' and the Copy To Output Directory to "Copy always."

对于每个新添加的 .foo 文件,我右键单击它,转到“属性”,并将“构建操作”设置为“资源”,将“复制到输出目录”设置为“始终复制”。

I want to be able to access those files both in XAML:

我希望能够在 XAML 中访问这些文件:

<Frame x:Name="bar" Source="/foofiles/page1.foo"/>

And in procedural code:

在程序代码中:

private void someFunc()
{
    bar.Source = new Uri("/foofiles/page1.foo");
}

But I just can't figure out why this doesn't work -- I get a "Format of the URI could not be determined."

但我就是不明白为什么这不起作用——我得到一个“无法确定 URI 的格式”。

In the code-behind, I tried doing this:

在代码隐藏中,我尝试这样做:

private void someFunc()
{
    bar.Source = new Uri("pack://application:,,,/foofiles/page1.foo");
}

which didn't throw an exception, but my main window crashed.

它没有抛出异常,但我的主窗口崩溃了。

In my thinking, if I add a file of any type to my project, and if I mark it as "Resource" in "Build Action," I should be able to use that file per my examples above. Also, I would like to use that file like this:

在我看来,如果我将任何类型的文件添加到我的项目中,并且如果我在“构建操作”中将其标记为“资源”,我应该能够按照上面的示例使用该文件。另外,我想像这样使用该文件:

private void someOtherFunc()
{
    System.IO.StreamReader reader = new System.IO.StreamReader("/foofiles/page1.foo");
    string bar = reader.ReadToEnd();
}

Any help would be appreciated... thanks in advance!

任何帮助将不胜感激...提前致谢!

回答by mkoertgen

Try adding the component-part to your Pack URI like this

尝试将component-part添加到您的 Pack URI 中

pack://application:,,,/AssemblyName;component/ResourceName

where AssemblyNameis the name of your assembly. So for your case, the following statement should work:

其中AssemblyName是程序集的名称。因此,对于您的情况,以下语句应该有效:

bar.Source = new Uri("pack://application:,,,/AssemblyName;component/foofiles/page1.foo");

More practically, try the relative pack uri notation:

更实际地,尝试使用相对的 pack uri 表示法:

bar.Source = new Uri("AssemblyName;component/foofiles/page1.foo", UriKind.Relative)); 

For stream reading resources use

对于流阅读资源使用

var streamResourceInfo = Application.GetResourceStream(uri);
using (var stream = streamResourceInfo.Stream)
{
   // do fancy stuff with stream
}