在DataContext中使用时无法删除文件 [英] Cannot delete file when used in DataContext

查看:83
本文介绍了在DataContext中使用时无法删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在屏幕上显示图像(基于本地计算机上文件的图像),用户可以根据需要将其删除。

My application shows images on screen (images based upon files on the local computer) and users can delete them if needed.

每次尝试删除文件时,它会导致以下错误消息:

Every time I try to delete a file it results in the following error message:

"The process cannot access the file 'C:\\Users\\Dave\\Desktop\\Duplicate\\Swim.JPG' because it is being used by another process."

我了解错误消息。

我有一个 UserControl ,它接受文件路径(通过构造函数中的参数),然后将其绑定到(UserControl) DataContext

I have a UserControl which accepts a file path (via a parameter in the constructor) and then binds it to it's (UserControl) DataContext.

作为调试此问题的一部分,我发现该问题是由于在UserControl中设置了DataContext。如果我从UserControl中删除 this.DataContext = this; ,则可以删除该文件。

As part of debugging this issue I have found the issue is due to setting the DataContext within the UserControl. If I remove this.DataContext = this; from within my UserControl then I can delete the file.

因此,我的TestUnit看起来像

So, my TestUnit looks like

        Ui.UserControls.ImageControl ic = new ImageControl(
           @"C:\Users\Dave\Desktop\Duplicate\Swim.JPG");

        try
        {
            File.Delete(@"C:\Users\Dave\Desktop\Duplicate\Swim.JPG");
        }
        catch (Exception ex)
        {
            Assert.Fail(ex.Message);
        }

后面的UserControl代码

The UserControl CodeBehind

    public ImageControl(string path)
    {
        this.FilePath = path;
        this.DataContext = this; // removing this line allows me to delete the file!
        InitializeComponent();
    }

    #region Properties

    private string _filePath;
    public string FilePath
    {
        get { return _filePath; }
        set
        {
            _filePath = value;
            OnPropertyChanged("FilePath");
        }
    }

如果有关系,我的UserControl XAML正在使用图片控件,绑定到 FilePath

If it matters, my UserControl XAML is using the 'Image' control, bound to 'FilePath'

我尝试在删除之前将UserControl设置为null,这无济于事。

I have tried making the UserControl null before deleting, this did not help.

我尝试将IDisposible接口添加到我的UserControl中,并添加到 Dispose()方法设置 this.DataContext = null; 但这没有帮助。

I have tried adding the IDisposible Interface to my UserControl and within the Dispose() method setting this.DataContext = null; but this did not help.

我在做什么错?如何删除该文件(或更准确地说,使其不使用)。

What am I doing wrong? How can I delete this file (or more accurately, make it unused).

推荐答案

问题不在于DataContext,而仅仅是WPF从文件加载图像的方式。

The problem is not the DataContext, but simply the way WPF loads images from files.

当您将Image控件的 Source 属性绑定到一个字符串时,包含一个文件路径,WPF内部从该路径创建一个新的BitmapFrame对象,基本上是这样的:

When you bind the Source property of an Image control to a string that contains a file path, WPF internally creates a new BitmapFrame object from the path basically like this:

string path = ...
var bitmapImage = BitmapFrame.Create(new Uri(path));

不幸的是,这会使WPF打开图像文件,因此您无法删除它。

Unfortunately this keeps the Image file opened by WPF, so that you can't delete it.

要解决此问题,必须将图像属性的类型更改为 ImageSource (或派生类型)并加载

To get around this you have to change the type of your image property to ImageSource (or a derived type) and load the image manually like shown below.

public ImageSource ImageSource { get; set; } // omitted OnPropertyChanged for brevity

private ImageSource LoadImage(string path)
{
    var bitmapImage = new BitmapImage();

    using (var stream = new FileStream(path, FileMode.Open))
    {
        bitmapImage.BeginInit();
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.StreamSource = stream;
        bitmapImage.EndInit();
        bitmapImage.Freeze(); // optional
    }

    return bitmapImage;
}

...
ImageSource = LoadImage(@"C:\Users\Dave\Desktop\Duplicate\Swim.JPG");

这篇关于在DataContext中使用时无法删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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