2

我有一个 XAML 文件,我使用它作为 ResourceDictionary 加载它

using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    // Read in ResourceDictionary File
                    ResourceDictionary dic =
                       (ResourceDictionary)XamlReader.Load(fs);

                    // Clear any previous dictionaries loaded
                    Application.Current.Resources.MergedDictionaries.Clear();
                    // Add in newly loaded Resource Dictionary
                    Application.Current.Resources.MergedDictionaries.Add(dic);
                    fs.Close();
                }

在那个 XAML 里面,我有一个 ImageBrush。当我阅读字典时,MyApplication.vshost.exe 现在正在使用构成 ImageBrush 的图像文件。即 ImageBrush 的 Source 属性中的文件。我需要删除该文件,但它给出了一个错误,因为该文件正在被另一个进程使用。我的问题是如何正确执行此操作,以便以后可以自由删除文件。

4

1 回答 1

2

您可以通过如下方式在资源中设置BitmapCacheOption.OnLoada :BitmapImageImageBrush

<ImageBrush x:Key="myImageBrush">
    <ImageBrush.ImageSource>
        <BitmapImage UriSource="..." CacheOption="OnLoad"/>
    </ImageBrush.ImageSource>
</ImageBrush>
于 2014-07-17T11:22:18.400 回答