通过 WPF 中的 XAML 代码加载外部图像?
我的 images
文件夹中的 WPF exe 文件旁边有一个图像 lock.png
。 现在,我要将其作为图像加载到 WPF 项目中,我使用了以下 XAML 代码:
<Image Stretch="Fill" Source="pack://siteoforigin:,,,/images/lock.png" />
它可以工作,但 Expression Blend
或 Visual Studio
不行。当我从事该项目时不会显示它。
在这些情况下我们如何展示外部图像呢?
I have an image lock.png
beside of my WPF exe file in the images
folder.
Now, I'm gonna load it into the WPF Project as an image, I've used the following XAML code:
<Image Stretch="Fill" Source="pack://siteoforigin:,,,/images/lock.png" />
It works, but Expression Blend
or Visual Studio
doesn't show it when I'm working on the project.
How can we show external images in these situations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试动态加载图像。这应该在 xaml 上:
并且在代码后面。在 Window_Loaded 或 Window 构造函数中:
Try to load your image dynamically. This should be on xaml:
And this in code behind. On Window_Loaded or in Window constructor:
使用如下格式:
项目;组件/ImagePath
例如
其中ImageDemo是项目名称,Image/ISIBAR.png是项目内部路径
Use format like:
Project;component/ImagePath
e.g.
Where ImageDemo is the project name, Image/ISIBAR.png is the path inside project
如果图像是相对于您的 EXE 位置,则只需执行“
如果图像不是相对的”,那么您将遇到更大的问题。仅当您实际将资源“打包”到程序集中时,pack 语法才有用。
松散图像和 Blend 的问题在于,Blend 将您的 exe 托管在它控制的临时目录中,并查找相对于该临时目录的图像,这将破坏您所依赖的任何路径。
If the image is relative to your EXE location just do
If the image isn't relative then you have a larger problem. pack syntax only useful if you are actually "packing" the resource into your assembly.
The problem with loose images and Blend is that Blend hosts your exe in a temp directory that it controls and looks for images relative to that temp directory, which will screw any pathing you are depending on.
我有同样的问题。
确保映像构建操作设置为资源。 (右键单击图像,然后转到属性,将构建操作设置为资源)
而不是 siteoforigin
此外,使用应用程序权限source :
https://stackoverflow.com/a/18175145/2672788
I had same question.
Make sure image build action is set to Resource. (right click on an image and then go to properties, set build action to resource)
Also, instead of siteoforigin use application authority
source:
https://stackoverflow.com/a/18175145/2672788
可能更容易:
记住反斜杠!
Could be easier:
Remember the backslashes!
很简单,您的图像未显示,因为应用程序运行后没有读取它。
解决此问题的一种快速方法是手动将图像从物理文件夹拖放到应用程序中的文件夹中。一旦到达那里,应用程序就能够读取它。
It is very simple, your image is not displaying because it is not being read by the application after you run it.
A quick way to get around this is by manually dropping the image from the physical folder to the folder in the application. Once it is there, the application will be able to read it.
您的主要 IDE 是 Visual Studio 吗?
如果是,为什么要手动执行此操作?在“属性”窗口中,您可以浏览要与图像组件一起使用的图像的方式
Is your primary IDE Visual Studio?
If yes, why do this manualy? In Propeties window you can just browse way to image you want to use with your Image component