简体   繁体   中英

Visual Studio designer can find resource image but not during runtime

I'm working with Visual Studio 2013 and .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.

<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

<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. 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. "yournamespace" is the namespace of your project.

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" />

This is invalid because a backslash is not allowd in a URL and a backslash must be escaped. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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