更新图像控件中的图像

我试图更新图像控件中的图像,该图像控件绑定到实现INotifyPropertyChanged的类。 我已经尝试了大多数与刷新位图缓存相关的方法,以便图像可以刷新,但似乎没有一个适用于我的情况。 图像控件在xaml文件中定义为: 并且在类后面的代码中是:

  private ImageSource imagechart = null; public ImageSource Chart { get { return imagechart; } set { if (value != imagechart) { imagechart = value; NotifyPropertyChanged("Chart"); } } } 

在事件之后,我现在使用以下代码设置图像:

 c.Chart = image; 

当我现在运行我的应用程序时,这将显示图像但在应用程序运行期间我更新图像,但调用此c.Chart = image; 显示初始图像。 我开始明白WPF会缓存图像,但声称要解决此问题的所有方法都适用于我。 其中一个对我不起作用的解决方案是将图像设置为图像源时覆盖(重新保存)图像的问题

尝试将Image属性的返回类型更改为Uri 。 Source Property上的TypeConverter应该完成其余的工作。 如果这不起作用,请validation资源是否已实际更改。

您可以使用Assembly.GetManifestResourceStreams从程序集中读取资源并解析字节。 然后使用File.WriteAllBytes手动将它们保存到输出目录,看看它是否具有预期的图像。

据我所知,在运行时(?)中无法更改Application Ressources(嵌入到程序集中)。 您正在引用程序集资源而不是包uri的输出资源。

谢谢大家通过他们的意见,我终于找到了解决这个问题的方法。 所以我的xaml仍然保持绑定为但在后面的代码中我更改了类属性图表以返回位图,如下所示:

  private BitmapImage image = null; public BitmapImage Chart { get { return image; } set { if (value != image) { image = value; NotifyPropertyChanged("Chart"); } } } 

这个类介意你实现了INotifyPropertyChanged 。 在我设置图像的点我现在正在使用此代码:

 BitmapImage img = new BitmapImage(); img.BeginInit(); img.CacheOption = BitmapCacheOption.OnLoad; img.CreateOptions = BitmapCreateOptions.IgnoreImageCache; //in the following code path is a string where i have defined the path to file img.UriSource = new Uri(string.Format("file://{0}",path)); img.EndInit(); c.Chart = img; 

这对我很有用,并在更新时刷新图像。