WPF 不刷新图像(缓存问题)

问题描述 投票:0回答:1

在我的应用程序中动态加载图像时遇到一个大问题。当我启动应用程序时,占位符图像源为空。当我单击按钮时,将创建图像并将其作为占位符源加载。当我再次单击时,会创建一个新图像,但会显示旧图像。图像的创建非常完美。我磁盘上的文件是它们应该的文件。

下面的函数是设置图片占位符的来源。

public void setImage(string path)
{
    BitmapImage img = new BitmapImage();
    img.BeginInit();
    img.UriSource = new Uri(path, UriKind.Relative);
    img.EndInit();
    
    //Set Refreshing Options
    img.CacheOption = BitmapCacheOption.None;
    img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;


    placeholder.Source = img;
}

BitmapCacheOption 和 BitmapCreateOptions 这两个选项不会改变任何内容。

你们有人可以帮助我吗?

wpf image cache-control
1个回答
0
投票
出于性能原因,WPF 在内部缓存图像。如果您两次使用相同的 Uri,并且期望每次都获得不同的图像(例如,如果 Uri 位于返回随机图像的 Web 服务器上),那么此缓存对您来说将是一个问题。

您可能需要创建一个 WebRequest 并手动下载图像,而不是依赖 Image 类来为您完成此操作。

另一种选择是以一种简单的方式更改 Uri,使其独一无二。例如,您可以附加 GUID 作为查询字符串。

© www.soinside.com 2019 - 2024. All rights reserved.