gpt4 book ai didi

c# - 从 XAML 加载 ResourceDictionary 也会将文件加载到内存中

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:39 25 4
gpt4 key购买 nike

我有一个 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 属性中的文件。我需要删除该文件,但它给出了一个错误,因为该文件正在被另一个进程使用。我的问题是如何正确执行此操作,以便以后可以自由删除该文件。

最佳答案

你可以设置BitmapCacheOption.OnLoadImageBrush 资源中的 BitmapImage 上,像这样写:

<ImageBrush x:Key="myImageBrush">
<ImageBrush.ImageSource>
<BitmapImage UriSource="..." CacheOption="OnLoad"/>
</ImageBrush.ImageSource>
</ImageBrush>

关于c# - 从 XAML 加载 ResourceDictionary 也会将文件加载到内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24801211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com