[英]wpf Image resources and visual studio 2010 resource editor
My motivation for this question is really just to specify an image to be used in a user control via a dependency property for ImageSource. 我对这个问题的动机实际上只是通过ImageSource的依赖属性指定要在用户控件中使用的图像。 I'm hitting some pain points involving the management, access, and unit testing for this.
我正在遇到一些涉及管理,访问和单元测试的痛点。
No, the resource editor is not a good tool for this. 不,资源编辑器不是一个好的工具。
In a WPF application the best way is to put all of your images in an "Images" directory and mark each one as a "Resource". 在WPF应用程序中,最好的方法是将所有图像放在“图像”目录中,并将每个图像标记为“资源”。 Then you can reference them directly in Image controls and elsewhere.
然后,您可以直接在图像控件和其他地方引用它们。
Here are the precise steps: 以下是精确的步骤:
Now you can reference your images easily in XAML: 现在,您可以在XAML中轻松引用图像:
<Image Source="Images/MyImage.png" />
Or in code: 或者在代码中:
var source = (BitmapSource)Application.LoadComponent(
new Uri("Images/MyImage.png", UriKind.Relative));
You can also reference images in external assemblies: 您还可以在外部装配中引用图像:
<Image Source="ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png" />
Which in code would be: 在代码中将是:
var source = (BitmapSource)Application.LoadComponent(
new Uri("ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png",
UriKind.Relative));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.