简体   繁体   English

Visual Studio设计器可以找到资源图像,但不能在运行时找到

[英]Visual Studio designer can find resource image but not during runtime

I'm working with Visual Studio 2013 and .NET 4.5. 我正在使用Visual Studio 2013和.NET 4.5。

I had an image linked as follows, with \\\\, and it was showing up in VS designer view but then not during runtime. 我有一个用\\\\链接的图像,如下所示,该图像显示在VS设计器视图中,但随后不在运行时显示。

<Image Grid.Column="1" Source="..\\Resources\\MyImage.bmp" Stretch="Uniform"/>

After quite a bit of trying, I got it to work by changing the \\\\ to / symbols, like so: 经过大量的尝试,我通过将\\\\更改为/符号使其工作,如下所示:

<Image Grid.Column="1" Source="../Resources/MyImage.bmp" Stretch="Uniform"/>

I'm just curious as to why the designer was able to find my image with the backslashes while runtime was not, and then why the forward slashes fixed this problem. 我很好奇为什么设计人员能够在运行时无法找到带有反斜杠的图像,以及为什么正斜杠可以解决此问题。 Anyone know? 有人知道吗 Thanks 谢谢

XAML XAML

<Image Source="pack://application:,,,/yournamespace;component/Resources/MyImage.bmp"/>

If I understand your problem correctly. 如果我正确理解您的问题。 Check your build action on your image. 检查图像上的构建操作。

That's given that your image resides in the Resources folder in your project. 假定您的图像位于项目的Resources文件夹中。 If you are wondering about that whole long string there before /Resources/MyImage.bmp, that's for allowing components to be used from other assemblies, and is the safe way of defining images in xaml. 如果您想知道/Resources/MyImage.bmp之前的整个长字符串,那是为了允许从其他程序集中使用组件,这是在xaml中定义图像的安全方法。 "yournamespace" is the namespace of your project. “ yournamespace”是项目的名称空间。

http://msdn.microsoft.com/en-us/library/aa970069.aspx http://msdn.microsoft.com/en-us/library/aa970069.aspx

在解决方案资源管理器中选择图像,并将其设置为“ Copy to Output DirectoryCopy if Newer并确保“构建操作”设置为“ Content

For an example: <img src="\\website\\file_name.jpg" /> 例如: <img src="\\website\\file_name.jpg" />

This is invalid because a backslash is not allowd in a URL and a backslash must be escaped. 这是无效的,因为URL中不允许使用反斜杠,并且必须转义反斜杠。 So when you go online with your code, all backslashes cannot be read. 因此,当您使用代码在线时,无法读取所有反斜杠。

The proper way is to write this: 正确的方法是这样写:

<img src="/website/file_name.jpg" />

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM