.net c#如何将一个图像分配给按钮的所有图像源

How to assign one image to all image sources of buttons
2021-06-20
  •  译文(汉语)
  •  原文(英语)

我正在使用WPF-Xaml创建界面,用户可以在其中选择图像并将其保存在数据库中,然后将此保存的图像分配给按钮ImageSource以及与所有其他"导航面板"项类似的对象,等等.

问题是:我每次都必须从数据库中检索图像,是否有其他可能性可以保存图像而不与数据库建立连接Application Settings?

我没有使用MVVM

速聊1:
因此,基本上,您想将图像保留到内存以外的其他位置吗?
速聊2:
是的 .我可以将其存储到WPF中的应用程序设置文件中吗
解决过程1

在视图模型中创建一个保存此图像的属性:

public class ViewModel{
   public ImageSource SharedImage{get;set;}
}

然后将需要显示此图像的控件绑定到此属性.这样,该图像就被设置一次,并被需要访问它的其他控件使用.

编辑 如果您想在应用程序首次使用图像时在文件系统中本地缓存文件,则可以执行以下操作:

public class ViewModel{
   private ImageSource _sharedImage;
   public ImageSource SharedImage{
      get{
         if(_sharedImage== null){
                  // Go load the image from somewhere
         }
         return _sharedImage;
      }
      set{
         if(_sharedImage == null){
              // Store the image contained in value into your local cache
         }
          _sharedImage = value;
      }
   }
}
速聊1:
如何在本地缓存文件?
速聊2:
刚设置属性时,只需将文件保存到磁盘-参见我的编辑
速聊3:
谢谢你.但是正如我告诉你的那样,我的应用程序中没有视图模型.所以我可以在UserControl中使用它.其次,我正在使用Devexpress,它提供了MVVM,但是它的界面不符合我的要求.我试图对其进行更改,但是由于对MVVM的了解很少,所以我搬回了.现在,您使我更加困惑地选择应该去的地方.:(

I am using WPF-Xaml I made interface in which user selects image and save it in database, then this saved image assigned to buttons ImageSource and similarly to all other Navigation Panel items, etc.

Problem is that: i have to retrieve image every time from database, Is there any other possibility to save image without connectivity to database i mean Application Settings ?

i am not using MVVM

Talk1:
So basically you want your image to be persisted to some place other than memory?
Talk2:
Yes . can i store this to application settings file in WPF
Solutions1

Create a property in your view model that will hold this image:

public class ViewModel{
   public ImageSource SharedImage{get;set;}
}

Then bind your controls that need to show this image to this property. This way the image is set once and used my times by other controls that need to access it.

EDIT If you want to cache your file locally in your file system when your application first uses the image you could do the following:

public class ViewModel{
   private ImageSource _sharedImage;
   public ImageSource SharedImage{
      get{
         if(_sharedImage== null){
                  // Go load the image from somewhere
         }
         return _sharedImage;
      }
      set{
         if(_sharedImage == null){
              // Store the image contained in value into your local cache
         }
          _sharedImage = value;
      }
   }
}
Talk1:
how to cache the file locally?
Talk2:
just save your file to disk when you first set your property - see my edit
Talk3:
thank you. but as i told you there is no view model in my application. so can i use this in UserControl. Secondly I am using Devexpress, it provides MVVM, but it's interface does not meet my requirement. i have tried to change it , but as has little knowledge about MVVM so i moved back. Now you make me more confuse to choose my way that where i should have to go. :(
转载于:https://stackoverflow.com/questions/18552236/one-image-to-be-image-source-of-all-buttons-and-windows-icons

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入