wpf 相对于程序集的本地文件的 URI

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

URI to local file relative to assembly

wpfxamlurirelative-path.net-assembly

提问by Sascha Lange

For example I have the following project structure:

例如我有以下项目结构:

Root\Core\Application.exe
Root\Modules\Assembly.dll
Root\Modules\Icons\Icon.png

My Application.exe loads the Assembly.dll. The Assembly.dll includes a WPF UserControl with an Image control. The Icon.png is not an embedded resource, it is a local file (Build: Content).

我的 Application.exe 加载了 Assembly.dll。Assembly.dll 包括一个带有 Image 控件的 WPF UserControl。Icon.png 不是嵌入资源,它是一个本地文件(构建:内容)。

I tried every possible URI in the Source-Property of the Image Control, but it never shows the image :-(

我在图像控件的源属性中尝试了所有可能的 URI,但它从未显示图像:-(

<Image Source="Icons\Icon.png" />
<Image Source="pack://application:,,,/Icons\Icon.png" />
<Image Source="pack://application:,,,/Assembly;component/Icons/Icon.png" />

etc.

等等。

How does the URI have to look like?

URI 必须是什么样子的?

回答by Colin Smith

This statement implies you can't refer to the Content files in your Assembly...(so only Content files that were specified in your Application can be resolved using "application://").

此语句意味着您不能引用程序集中的内容文件...(因此,只能使用“application://”解析应用程序中指定的内容文件)。

http://msdn.microsoft.com/en-us/library/aa970069(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/aa970069(v=vs.85).aspx

Content files in referenced assemblies are not included because they are unsupported by WPF. Pack URIs for embedded files in referenced assemblies are unique because they include both the name of the referenced assembly and the ;component suffix. Pack URIs for site of origin files are unique because they use the are the only pack URIs that use the siteoforigin:/// authority.

不包括引用程序集中的内容文件,因为 WPF 不支持它们。引用程序集中嵌入文件的 Pack URI 是唯一的,因为它们包含引用程序集的名称和 ;component 后缀。源文件站点的包 URI 是唯一的,因为它们使用的是唯一使用 siteoforigin:/// 权限的包 URI。

You might get somewhere with "siteoforigin" which refers to the location where your .exe is running.

您可能会找到带有“siteoforigin”的地方,它指的是 .exe 运行的位置。

Source="pack://siteoforigin:,,,/../Modules/Icons/Icon.png"

or

或者

Source="pack://siteoforigin:,,,/Icons/Icon.png"

Though it might not support the relative path.

虽然它可能不支持相对路径。

Give this a shot as well:

也试一试:

Source="../Modules/Icons/Icon.png" 

And this:

和这个:

Source="Icons/Icon.png" 

One tip when experimenting with Pack URIs is to build them using the PackUriHelper, so that you conform to the proper syntax/semantics.

尝试使用 Pack URI 时的一个技巧是使用 构建它们PackUriHelper,以便您符合正确的语法/语义。

See some related posts:

看一些相关的帖子:



Another idea....

另一个想法....

You could define your own MarkupExtension which helped build an Absolute path to your png files.

您可以定义自己的 MarkupExtension,这有助于构建 png 文件的绝对路径。

You would have a global configuration setting which got set just prior to the loading of your DLL with LoadFrom.

您将拥有一个全局配置设置,该设置恰好在使用 LoadFrom 加载 DLL 之前设置。

Then you would make your XAML use the extension:

然后你会让你的 XAML 使用扩展名:

Source={local:MyMarkupExtensionPathBuilder Icon.png}

Use this as a starting point...in your case you don't want to use "application://" though...you want to build the Uri as a straight absolute "file" path type URI e.g. "C:\Program Files\myapplication\Root\Modules\Icons\icon.png".

以此为起点……在您的情况下,您不想使用“application://”……但您想将 Uri 构建为直接的绝对“文件”路径类型 URI,例如“C:\ Program Files\myapplication\Root\Modules\Icons\icon.png”。

You can get your MarkupExtension to return the BitmapSource directly (or just get it to return a string...which then a TypeConverter resolves to a BitmapSource anyway).

你可以让你的 MarkupExtension 直接返回 BitmapSource (或者只是让它返回一个字符串......然后 TypeConverter 无论如何都会解析为 BitmapSource )。

(you could also do something similar with a Binding that used a Converter to concatenate the 2 parts of the path, or make your ViewModel do the concatenation if you use one...the markupextension is a neater way to do it though)

(你也可以用一个 Binding 做一些类似的事情,它使用一个 Converter 来连接路径的 2 部分,或者如果你使用一个,让你的 ViewModel 进行连接......不过,markupextension 是一种更简洁的方法)