删除XAML使用的图像文件 [英] Delete image file used by XAML

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

问题描述

我正在尝试删除WPF中的图像文件,但WPF会锁定该文件。

I'm trying to delete a Image file in WPF, but WPF locks the file.

    <Image Source="C:\person.gif" x:Name="PersonImage">
        <Image.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Delete..." x:Name="DeletePersonImageMenuItem" Click="DeletePersonImageMenuItem_Click"/>
            </ContextMenu>
        </Image.ContextMenu>
    </Image>

Click处理程序如下所示:

And the Click handler just looks like this:

    private void DeletePersonImageMenuItem_Click(object sender, RoutedEventArgs e)
    {
        System.IO.File.Delete(@"C:\person.gif");
    }

但是,当我尝试删除文件时,它被锁定且无法删除。

But, when I try to delete the file it is locked and cannot be removed.

有关如何删除文件的任何提示?

Any tips on how to delete the file?

推荐答案

首先从PersonImage控件中删除它然后删除图像。希望会有所帮助。
正如您已在源代码中分配给控件,并在不取消分配控件源的情况下将其删除。

First Remove it from the PersonImage control then delete the image. Hope that will help. As you have assigned to the control in source, and remove it without unassigning the control source.

PersonImage.Source = null; 
System.IO.File.Delete(@"C:\person.gif"); 

希望这会有所帮助。

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

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