WPF 使用相对路径从程序集中的任何位置访问资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43780994/
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 14:04:59  来源:igfitidea点击:

WPF accessing a resource from anywhere in the assembly using relative path

c#wpfrelative-path

提问by Marcin Bator

In my application I am creating a ton of custom controls that I'm then using in different windows. The structure of my project looks something like this:

在我的应用程序中,我创建了大量自定义控件,然后在不同的窗口中使用这些控件。我的项目结构如下所示:

  • Controls (folder)
    • MemberList (class)
  • Resources (folder)
  • Windows (folder)
    • Window1.xaml
    • Window2.xaml
    • Helper windows (folder)
      • Window3.xaml
  • 控件(文件夹)
    • 成员列表(类)
  • 资源(文件夹)
  • 窗户(文件夹)
    • 窗口1.xaml
    • Window2.xaml
    • 辅助窗口(文件夹)
      • Window3.xaml

I am creating classes (controls) that I store in the Controls folder. If I want to use an image in the control, I access it like so:

我正在创建存储在 Controls 文件夹中的类(控件)。如果我想在控件中使用图像,我可以像这样访问它:

new Uri("../Resources/my_image.png", UriKind.Relative)

Sometimes though, I want to use the same control in multiple windows and sometimes these windows are stored in different folders. If I use the control in say Window3 that is stored in another folder, the image is not displayed.

有时,我想在多个窗口中使用相同的控件,有时这些窗口存储在不同的文件夹中。如果我使用存储在另一个文件夹中的 Window3 中的控件,则不会显示图像。

If I change the Uri to this: (adding another "go back" command in the path)

如果我将 Uri 更改为:(在路径中添加另一个“返回”命令)

new Uri("../../Resources/my_image.png", UriKind.Relative)

the image is being displayed in the Window3, but it is not displayed in the Window1 or Window2.

图像在Window3 中显示,但在Window1 或Window2 中未显示。

Is there a way to create paths that are relative to the main executable and not relative to the window that is displaying the control?

有没有办法创建相对于主可执行文件而不是相对于显示控件的窗口的路径?

EDIT: I forgot to mention one important part. Because of the fact that while starting this application I was just learning how to use WPF, I messed one thing. My resources folder is not in the folder that contains the main executable.

编辑:我忘了提到一个重要的部分。因为在启动这个应用程序时我只是在学习如何使用 WPF,我搞砸了一件事。我的资源文件夹不在包含主要可执行文件的文件夹中。

My file structure looks like this:

我的文件结构如下所示:

  • bin
    • Debug
      • MyApplication.exe
  • Resources
  • 垃圾桶
    • 调试
      • 我的应用程序
  • 资源

I was trying to use the path that everyone suggested:

我试图使用每个人都建议的路径:

 new Uri("pack://application:,,,/../../my_image.png", UriKind.RelativeOrAbsolute)

but I get the exception:

但我得到了例外:

Cannot locate resource 'my_image.png'.

找不到资源“my_image.png”。

I could try to relocate the Resources folder, but it would mess up all other paths. Is there any way to go "back" in folder structure in the "pack" uri? If not, what would be the best way to relocate a folder so all paths do not get messed up?

我可以尝试重新定位 Resources 文件夹,但它会弄乱所有其他路径。有没有办法在“pack”uri的文件夹结构中“返回”?如果没有,重新定位文件夹的最佳方法是什么,这样所有路径都不会被弄乱?

回答by Colin Smith

Use a "pack" uri...so you would use:

使用“包”uri...所以你会使用:

pack://application:,,,/Resources/my_image.png

so like this:

像这样:

new Uri("pack://application:,,,/Resources/my_image.png");

to refer to your "image" from ANY of your Windows/Controls.

从您的任何 Windows/控件中引用您的“图像”。

If you will have other "projects"/"assemblies"...which you want to be able to refer to the resource then use:

如果您有其他“项目”/“程序集”...您希望能够引用资源,请使用:

pack://application:,,,/AssemblyNameContainingResource;component/Resources/my_image.png

And when you are adding your images in your Resourcefolder....make sure you use Resourceas the Build Action.

而当你在你的加入你的图片Resource文件夹....确保您使用ResourceBuild Action

enter image description here

在此处输入图片说明

回答by 3615

Use Pack Uri Scheme

使用Pack Uri Scheme

Resource File Pack URIs - Local Assembly

The pack URI for a resource file that is compiled into the local assembly uses the following authority and path:

Authority: application:///.

Path: The name of the resource file, including its path relative to the root of the local assembly project folder.

The following example shows the pack URI for a XAML resource file that is located in the root of the local assembly's project folder:

pack://application:,,,/ResourceFile.xaml

The following example shows the pack URI for a XAML resource file that is located in a subfolder of the local assembly's project folder:

pack://application:,,,/Subfolder/ResourceFile.xaml

资源文件包 URI - 本地程序集

编译到本地程序集中的资源文件的包 URI 使用以下权限和路径:

Authority: application:///.

Path: The name of the resource file, including its path relative to the root of the local assembly project folder.

以下示例显示位于本地程序集项目文件夹根目录中的 XAML 资源文件的包 URI:

pack://application:,,,/ResourceFile.xaml

以下示例显示位于本地程序集项目文件夹的子文件夹中的 XAML 资源文件的包 URI:

pack://application:,,,/Subfolder/ResourceFile.xaml

回答by toadflakz

Short answer:Use pack URI notation.

简短回答:使用包 URI 表示法。

Example:

例子:

Your URI would look similar to: pack://application:,,,/Resources/my_image.pngregardless of where in the XAML you use it.

您的 URI 将类似于:pack://application:,,,/Resources/my_image.png无论您在 XAML 中的哪个位置使用它。

Documentation:

文档:

MSDN - Pack URIs in WPF

MSDN - 在 WPF 中打包 URI

回答by Fred

In a submodule such as SubModuleA, in the viewmodel of SubModuelA, when you want to access the resource file of SubModuelA instead of the started project.

SubModuleASubModuelA的viewmodel中,比如 ,当你想访问SubModuelA的资源文件而不是启动的项目时。

For example, the resource file's directory is StartApp/SubModuelA/Resource/Data/JsonData.json, In viewmodel of SubModuelA it's path should be

例如资源文件的目录是StartApp/SubModuelA/Resource/Data/JsonData.json,在SubModuelA的viewmodel中它的路径应该是

string jsonFile="../../../SubModuleA/Resource/Data/JsonData.json"

I validated this and wrote data into the jsonFile.

我验证了这一点并将数据写入 jsonFile。