使用正确的 pack://URI 格式 [英] Using the correct pack:// URI Format
问题描述
我有一个 WPF 类库,其中包含一个文件夹图像";包含一个图像.在 WPF 应用程序中,我不会使用
I have a WPF class Library, containing a Folder "Images" that contains an Image. In a WPF application I wont to refer to the image in that Folder using
<Image Name="image" Grid.Row="0" Source="pack://application:,,,/ImageService.dll;Component/Images/image.jpg"/>
图像被标记为资源";并且我还把它添加到了类库Project的资源中.经过一番搜索我发现,我必须将dll复制到exe目录中.
The image ist marked as "Ressource" and I also added it to the resources of the class library Project. After some searching I found out, that I have to copy the dll into the exe directory.
在设计期间,图像显示正确.启动可执行文件后,图像的 source
属性为空.输出窗口中没有错误消息和提示.
During designtime the image is displayed correctly. After launching the executable the source
property of the image is null. No error message and no hints in the output window.
看来,pack uri 的格式不正确,因此运行时引用了错误的位置.有没有办法指定相对于可执行文件的程序集名称,使用....\AssemblyName.dll"?或绝对到文件系统根目录.
It seems, that the Format of the pack uri is incorrect, so the runtime refers to a wrong place. Is there a way to specify the assembly name relative to the executable, using "....\AssemblyName.dll" or absolute to file systems root.
不幸的是,我无法通过代码分配图像源,因为所有内容都通过 XAMLWriter - XAMLReader 构造从 dll 到执行程序集.
Unfortunately I cannot assign the imagesource via code, because all the content goes through XAMLWriter - XAMLReader construct from the dll to the executing assembly.
推荐答案
您不必将图像复制到输出目录.确保将 Build Action
标记为 Resource
并设置 Copy to Output Directory
到 false
.
You don't have to copy image to output directory. Make sure you mark the Build Action
as Resource
and set Copy to Output Directory
to false
.
此外,您可以在包 URI 中跳过程序集名称,以防 XAML 和图像驻留在同一个程序集中.如果它们驻留在不同的程序集中,您只需提供程序集名称(从路径中删除 dll).
Also, you can skip assembly name in pack URI in case XAML and image resides in same assembly. In case they resides in different assembly, you have to give only assembly name (remove dll from path).
<Image Source="pack://application:,,,/ImageService;Component/Images/image.jpg"/>
这篇关于使用正确的 pack://URI 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!