在设计时 pack uri 有效,但在运行时无效?

发布于 2024-11-26 11:46:37 字数 427 浏览 1 评论 0原文

我正在将按钮的内容设置为图像。它看起来像这样:

<Button>
   <Image Source="pack://application:,,,/NavigationImages/nav_up_left.png" />
</Button>

在我的项目中,我有一个名为 NavigationImages 的子文件夹,该文件夹中是图像文件 nav_up_left.png。

当我查看设计器时,会出现图像,但是在运行时我收到 IOException 错误,指出它无法找到资源。

构建操作设置为资源。

事实上,这在一个项目中效果很好。但是当我将其复制到另一个项目时,它失败了。这似乎是一个非常简单的问题,但我发现自己被难住了,准备开始拔头发。 @_@

您的想法和建议将不胜感激!

I'm setting a Button's content to an Image. It looks something like this:

<Button>
   <Image Source="pack://application:,,,/NavigationImages/nav_up_left.png" />
</Button>

In my project I have a subfolder named NavigationImages and within that folder is the image file nav_up_left.png.

When I view the Designer the image appears, however during runtime I get an IOException error saying it cannot locate the resource.

The Build Action is set to Resource.

Actually, this worked fine in one project. But when I copied it over the another project it fails. This seems like an incredibly simple problem, but I find myself stumped and ready to start pulling out hair. @_@

Your thoughts and suggestions will be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一百个冬季 2024-12-03 11:46:37

呃,我想通了……有点。

我将该 xaml 代码从输出类型为 Windows 应用程序的一个项目复制到输出类型为类库的另一个项目。

我当时没有想到这一点,但显然当输出类型是类库时,包 URI 需要更改。

因此,我将其更改为 "/ProjectName;component/NavigationImages/nav_up_left.png",而不是 "pack://application:,,,/NavigationImages/nav_up_left.png"现在一切正常。

我不是 100% 清楚为什么这是有效的而不是前者。我已通读 有关包 URI 的 MSDN 文档在 WPF 中,但也许我误解了一些东西。

如果有人可以给我一个很好的解释,为什么我之前的内容在具有输出类型类库的项目中不起作用,我将不选中此答案。

我可能错过了一些非常简单的东西。 @_@

Whelp, I figured it out...kinda.

I copied that xaml code from one project where the output type is Windows Application, to another project where the output type is Class Library.

I didn't think of it at the time, but apparently when the output type is a Class Library the pack URI needs to change.

So instead of "pack://application:,,,/NavigationImages/nav_up_left.png" I changed it to "/ProjectName;component/NavigationImages/nav_up_left.png" and now it's working just fine.

I'm not 100% clear why this is works and not the former. I've read through the MSDN documentation on pack URIs in WPF but perhaps I misinterpreted something.

I'll leave this answer unchecked in the event someone can give me a good explanation why what I previously had doesn't work in a project with output type Class Library.

I'm probably missing something really simple. @_@

ゞ花落谁相伴 2024-12-03 11:46:37

我只是在这个同样的问题上挣扎了很长一段时间,我认为原文中出现问题的部分原因是缺少“组件”一词。例如,我有,

myBitmapImage.UriSource = new Uri(@"pack://application:,,,/MyApp;images/mona2.jpg");

应该

... = new Uri(@"pack://application:,,,/MyApp;component/images/mona2.jpg");

“组件”一词不是路径名的一部分,尽管它出现了——它是一个必须存在的字符串文字。为什么?我猜有人认为这是个好主意。

对于那些在事情的另一部分苦苦挣扎的人,“MyApp”怎么样?这就是议会的名称。右键单击项目名称,选择“属性...”,在“应用程序”选项卡下,您将看到“程序集名称:”字段。

如果你不想寻找它(或者担心它可能会改变,破坏你的代码),你可以这样做:

String appUri = @"pack://application:,,,/" + 
                  System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ";";
String path = appUri + "component/images/mona2.jpg";
myBitmapImage.UriSource = new Uri(path);

我承认,代码不是很漂亮——它显然可以缩短——但它会让你明白我希望你要去哪里。请记住将图像文件的“Build”属性设置为“Resource”!

I just struggled with this same problem for quite a while, and I think that part of what was going wrong in the original was the missing word "component". I, for instance, had

myBitmapImage.UriSource = new Uri(@"pack://application:,,,/MyApp;images/mona2.jpg");

but should have had

... = new Uri(@"pack://application:,,,/MyApp;component/images/mona2.jpg");

The word "component" is not part of the pathname, despite its appearance -- it's a string literal that has to be there. Why? Someone thought it'd be a good idea, I guess.

And for those struggling with another part of the thing, what about "MyApp"? That's the name of the Assembly. Right-click on your project name, select "Properties...", and under the "Application" tab you'll see the "Assembly name:" field.

If you don't feel like searching for that (or worry that it might change, breaking your code), you can do this:

String appUri = @"pack://application:,,,/" + 
                  System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ";";
String path = appUri + "component/images/mona2.jpg";
myBitmapImage.UriSource = new Uri(path);

Not very pretty code, I admit -- it can clearly be shortened -- but it'll gets you where you need to go, I hope. Remember to set the "Build" property on your image file to "Resource"!

零崎曲识 2024-12-03 11:46:37

只是为了了解您的情况发生了什么。第二个包uri。那个有效的。适用于位于主机应用程序之外的程序集中的资源。听起来,主机应用程序正在从相关类库加载此资源?

您可以在此处查看 pack uri 方案的差异:
MSDN Pack URI 方案

引用以下资源时,URI 略有变化主程序集,并从另一个程序集引用一个程序集。

另外, pack://application:,,, 包含所谓的“权限”,省略它基本上会使其成为相对路径,两者在假定应用程序权限的大多数情况下都是有效的。

编辑:基本上是因为 /Subfolder/Resource.xaml(.jpg 等) 和 /Assembly;component/Resource.xaml 非常相似,后者告诉解析器/加载器它正在引用的程序集中查找,而不是在主应用程序的程序集中查找。 (我想这有助于加快搜索速度)。

Just to shine a light on what was happening in your situation. The second pack uri. The one that worked. Is meant for resources located in an assembly other than the host application. By the sounds of it, the host application was loading this resource from the Class Library in question?

You can see the differences in the pack uri schemes here:
MSDN Pack URI Scheme

The uri changes slightly when referencing a resource from the main assembly, and referencing one from another assembly.

Also, the pack://application:,,, includes what is referred to as the "authority", to omit it would basically make it a relative path, both are valid in most cases where the application authority is assumed.

EDIT: basically because /Subfolder/Resource.xaml(.jpg etc.) and /Assembly;component/Resource.xaml are very similar, the latter tells the parser/loader that it's looking in a referenced assembly, not in the main application's assembly. (I imagine this helps speed up the search).

述情 2024-12-03 11:46:37

解决此问题的另一种解决方案是:

将图像构建操作设置为“资源”并且重新构建后,导航到的属性目的。属性窗口将提供一个 ... 文件资源浏览器,然后选择您的图像 Source=".​​.." 属性; 将被正确填写。

One other solution to getting this right:

Once your image Build Action is set to 'Resource' and you have rebuilt, navigate to the properties of your <Image /> object. The properties window will provide a ... file resource browser, whereupon selecting your image the Source="..." attribute of your <Image /> will be correctly filled in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文