我怎样才能图像存储在一个类库,并从任何类访问 [英] How can i store an image in a class library and access it from any class

查看:143
本文介绍了我怎样才能图像存储在一个类库,并从任何类访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的解决方案在一个文件夹'形象'的几个图像。从我的库中的类中,我想我决定要在运行时使用的图像。旧的方式,我会做到这一点从用户控件的代码中的背后,是通过加载所有的图像在类的构造函数,像这样:

I have a few images in a folder 'Images' in my solution. From within a class in my library I would like to decide which image I should use at runtime. The old way i would do this from within a user control's code behind is by loading all of the images in the class's constructor like so:

myImage = FindResource("MyImageResource") as BitmapImage;



然后在运行时使用的图像判断。

Then determining at runtime which image to use.

但是,这似乎并没有从任何类中工作,在我的类库何况这似乎违背,我读过有关MVVM所有。

But this doesn't seem to work from within any class in my class library not to mention it seems to go against all that I've read about MVVM.

上什么,我试图做更多的信息
基本上我有存储的所有信息模型类其中已插入诸如驱动类型和它的名称的驱动器。

A bit more information on what i am trying to do Essentially i have a model class which stores all of the information on a drive which has been inserted such as drive type and its name.

当任何驱动器插入或取出用于监控视图模型类。

A view model class which monitors when any drive is inserted or removed.

和这是视图类。保存按钮类似系统的驱动器的集合面板

And the view class which is a panel that holds a collection of buttons resembling the drives of the system.

澄清我的问题:

我应该如何存储和在WPF类库加载图像?

How should i store and load images in a WPF class library?

在哪个类我应确定(在运行时)要使用的图像(在MVVM模式)?

In which class should i determine (at runtime) which image to use (in the MVVM pattern)?

推荐答案

我已经决定迄今做的就是给模型类BitmapImage的财产,我在它的构造函数设置为表示使用该行的驱动器类型的图像:

What i have decided to do thus far is to give the model class a BitmapImage property which i set in it's constructor to the Image that represents its drive type using this line:

DiskImage  = new BitmapImage(new Uri("/MyApp;component/Images/DiskDrive.png", UriKind.Relative));

这篇关于我怎样才能图像存储在一个类库,并从任何类访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆