如何访问 MVVM WPF 应用程序中的图像资源?
我将图像资源集成到 WPF 程序集已经有一段时间了,但我从未真正找到一种真正漂亮的方法将它们绑定到我的 MVVM 应用程序中。
所以我想知道你们这些 stackoverflow 用户是否可以分享一下他们自己在程序集资源方面的实践:
- 如何在 C#/VB 和 XAML 中引用它们?
- 当从代码中公开它们时,您公开什么类型? (BitmapImage、Uri、字符串...)
- 您更喜欢从视图引用它们还是通过视图模型绑定它们?
- 您是否使用特定的程序集来存储它们?你如何组织它们?
感谢您的帮助 ;-)
I have been integrating image resources to my WPF assemblies for quite a time, but I never really found a really pretty way of binding them in my MVVM applications.
So I was wondering if some you, stackoverflow users, could share their own practice when it comes to assembly ressources:
- How do you reference them in C#/VB and XAML?
- When exposing them from code, what type do you expose? (BitmapImage, Uri, string, ...)
- Do you prefer referencing them from the view or binding them through view-model?
- Do you use a specific assembly to store them? How do you organize them?
Thanks for your help ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我所做的...
Images
。您可以将它们保留在任何程序集中。Build Action
属性设置为Resource
类型。ViewModel 将图像名称保存在
string
中(例如属性MyImageName
),当绑定到时,我将其转换为相对路径XAML 中
...Image
对象的 >Source其中
MyApplicationName
是具有Images 文件夹下。
在 XAML 中,假设视图模型实例绑定到整个视图的数据上下文和/或至少绑定到图像的数据上下文......
但这就是引用图像的典型方法之一通过 MVVM 在 WPF 项目中。其他方式包括加载图像的二进制流(从
resx
或Content
类型图像或从二进制数据库)。在这种情况下,您的转换器会将其转换为BitMapImageSource
。This is what I do...
Images
in project's root folder. You can keep them in any assembly.Build Action
property set toResource
type.The ViewModel holds image name in
string
(say propertyMyImageName
) and I convert it as a relative path when bound to theSource
ofImage
object in XAML...where
MyApplicationName
is the short assembly's name that is having theImages
folder under it.In XAML, assuming the view model instance is bound to the data context of the entire view and / or atleast to the image's data contect ....
But then thats one of the typical ways to refer images in a WPF project via MVVM. Other ways include an Image's binary stream loaded (from
resx
orContent
type images or from binary database). In such case your converter will convert it into aBitMapImageSource
.