如何访问MVVM WPF应用程序中的图像资源?(How do you access to your image resources in MVVM WPF applications?)

编程入门 行业动态 更新时间:2024-10-20 20:57:17
如何访问MVVM WPF应用程序中的图像资源?(How do you access to your image resources in MVVM WPF applications?)

我一直在将图像资源整合到我的WPF程序集中,但我从未真正发现过将它们绑定到我的MVVM应用程序中的非常好的方法。

所以我想知道是否有一些你,stackoverflow用户,可以分享他们自己的做法,当涉及到大会资源:

你如何在C#/ VB和XAML中引用它们? 当从代码暴露他们时,你会暴露什么类型? (BitmapImage,Uri,string,...) 你喜欢从视图中引用它们还是通过视图模型来绑定它们? 你使用特定的程序集来存储它们吗? 你如何组织他们?

谢谢你的帮助 ;-)

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 ;-)

最满意答案

这就是我所做的...

图像保存在某个文件夹中,例如项目根文件夹中的Images 。 您可以将它们放在任何装配中。 单个图像的Build Action属性设置为Resource类型。

ViewModel在string保存图像名称(比如属性MyImageName ),并将其转换为绑定到XAML中Image对象Source时的相对路径...

public class ImagePathConverter { public void Convert(...) { return "pack://application:,,,/MyApplicationName;component/Images/" + value.ToString(); } }

其中MyApplicationName是具有其下的Images文件夹的短程序集名称。

在XAML中,假设视图模型实例绑定到整个视图的数据上下文和/或至少与图像的数据竞争....

<Image Source="{Binding Path=MyImageName, Converter={StaticResource ImagePathConverter}}" />

但那是通过MVVM在WPF项目中引用图像的典型方法之一。 其他方式包括加载的图像二进制流(来自resx或Content类型图像或来自二进制数据库)。 在这种情况下,您的转换器会将其转换为BitMapImageSource 。

This is what I do...

Images are kept in some folder such as Images in project's root folder. You can keep them in any assembly. Individual image has Build Action property set to Resource type.

The ViewModel holds image name in string (say property MyImageName) and I convert it as a relative path when bound to the Source of Image object in XAML...

public class ImagePathConverter { public void Convert(...) { return "pack://application:,,,/MyApplicationName;component/Images/" + value.ToString(); } }

where MyApplicationName is the short assembly's name that is having the Images 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 ....

<Image Source="{Binding Path=MyImageName, Converter={StaticResource ImagePathConverter}}" />

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 or Content type images or from binary database). In such case your converter will convert it into a BitMapImageSource.

更多推荐

本文发布于:2023-07-25 05:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1256770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   图像   资源   WPF   MVVM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!