WPF MVVM模式图片占用问题

在很久以前就遇到这个问题,当时解决了,这过了几年,又遇到这个问题,这里做个总结,防止下次再踩坑了,也顺便帮助一下同样遇到这个问题的朋友 。

出现这个问题的原因是:将文件路径绑定到Image的Source上

ImageSource 属性实际上需要的是一个ImageSource类型,但是可以直接将string(文件路径)绑定到Source上。我查了一下WPF的源码,并没有找到是如何在绑定时将string转换成ImageSource。不过原理应该还是下面这样,所以会导致图片文件占用。

复制代码
Image image = new Image();
image.BeginInit();
image.UriSource = new Uri(xxx);
image.EndInit();

下面看一下文件占用的情况:

如下:

MainWindow.xaml

复制代码
1  <Grid>
2         <Image Source="{Binding ImagePath}"/>
3  </Grid>

MainWindow.xaml.cs

复制代码
 1  public partial class MainWindow : Window
 2     {
 3         public MainWindow()
 4         {
 5             InitializeComponent();
 6 
 7             var viewModel = new MainWindowViewModel();
 8             this.DataContext = viewModel;
 9 
10             //假设D盘下有个d.jpeg文件
11             viewModel.ImagePath = @"D:\\d.jpeg";
12         }
13     }

MainWindowViewModel.cs

复制代码
 1 public class MainWindowViewModel : INotifyPropertyChanged
 2     {
 3         private string imagePath;
 4 
 5         public string ImagePath
 6         {
 7             get => imagePath;
 8             set
 9             {
10                 imagePath = value;
11                 RaiseChanged("ImagePath");
12             }
13         }
14 
15         public event PropertyChangedEventHandler PropertyChanged;
16 
17         public void RaiseChanged(string propertyName)
18         {
19             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
20         }
21     }

运行后可以看到D盘下的d.jpeg文件是不能删除的

解除文件占用的原理是在初始化时,就将图片加载到内存中,如下:

复制代码
1 image.BeginInit();
2 image.CacheOption = BitmapCacheOption.OnLoad;
3 image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
4 image.UriSource = new Uri(path, UriKind.Absolute);
5 image.EndInit();

了解原理后,我们就可以建立一个ImageSourceConvert来解除文件占用的问题

复制代码
 1 public class ImageSourceConverter : IValueConverter
 2     {
 3         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 4         {
 5             if (value == null)
 6                 return DependencyProperty.UnsetValue;
 7 
 8             var path = value.ToString();
 9             BitmapImage image = new BitmapImage();
10             image.BeginInit();
11             image.CacheOption = BitmapCacheOption.OnLoad;
12             image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
13             image.UriSource = new Uri(path, UriKind.Absolute);
14             image.EndInit();
15             return image;
16         }
17 
18         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
19         {
20             throw new NotImplementedException();
21         }
22     }

MainWindow.xaml

复制代码
1  <Window.Resources>
2         <local:ImageSourceConverter x:Key="ImageSourceConverter"/>
3     </Window.Resources>
4     <Grid>
5         <!--<Image Source="{Binding ImagePath}"/>-->
6         <Image Source="{Binding ImagePath,Converter={StaticResource ImageSourceConverter}}"/>
7     </Grid>

示例代码

相关推荐
绿龙术士18 小时前
构建现代化WPF应用:数据驱动开发与高级特性解析
c#·wpf
wangnaisheng2 天前
【WPF】Opacity 属性的使用
wpf
姬激薄3 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)3 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心3 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo4 天前
9.1.领域驱动设计
wpf
大道随心4 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠4 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go4 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet4 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf