2016-07-06 98 views
0

我有我的对象“会话”的网格视图。在用户选择时,每个会话的图像都会显示在我的GUI上的图像中。我有这个程序,用户,用户SelectedSession,它删除任何现有的图像注意到一个新的图像frameSource保存它本地,然后设置私有成员变量ImagePath作为其路径:为什么我的图片更新不是“绑定”的?

public Session SelectedSession { get { return selectedSession; } set { SetValue(ref selectedSession, value); } } 
    public BitmapSource SessionImage { get { return sessionImage; } private set { SetValue(ref sessionImage, value); } } 

//.. 
    public void TakeSessionImage(BitmapSource frameSource) 
    { 
     if (SelectedSession != null) 
     { 
      if (File.Exists(SelectedSession.ImagePath)) 
       File.Delete(SelectedSession.ImagePath); // delete old - works 

      SelectedSession.ImagePath = FileStructure.CurrentSessionPath + "\\" + SelectedSession.Name + ".png"; // set ImagePath - works - Technically it does not change. I kept it if I needed later to add anything to the file name like GUID or whatever 
      ImageIO.RotateAndSaveImage(SelectedSession.ImagePath, (WriteableBitmap)frameSource, -270); // save new image - works 
      SessionImage = SelectedSession.LoadImageFromFile(); // binded the image to display 
     } 
    } 

绑定在XAML:

<Image x:Name="currentSessionImage" Source="{Binding SessionImage}"/> 

在 “Session.cs” 类:

public BitmapImage LoadImageFromFile() 
{ 
    if (File.Exists(ImagePath)) // image path is correct 
    { 
     try 
     { 
      BitmapImage image = new BitmapImage(); 
      image.BeginInit(); 
      image.CacheOption = BitmapCacheOption.OnLoad; 
      image.UriSource = new Uri(ImagePath); 
      image.EndInit(); 
      return image; 
     } 
     catch 
     { 
      return null; 
     } 
    } 
    return null; 
} 

这已被调试并且所有语句均有效。 SessionImage是我的GUI上“绑定”到图像的属性。然而,一个非常奇怪的行为正在发生:

  • 这是删除旧的图像文件,我可以看到,在Windows资源管理器中,该文件已经消失。
  • 它保存新的一个正确
  • 它是从正确路径
  • 不过,它只显示我曾经花了很形象第一次加载新的形象。

无论发送什么新图像。它总是显示我第一次拍摄的相同图像。任何人都可以为我检查一下吗?我验证了整个代码,所有的值都是合乎逻辑的。任何地方都没有语法错

编辑:

protected virtual bool SetValue<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) 
{ 
    if (!EqualityComparer<T>.Default.Equals(storage, value)) 
    { 
     storage = value; 
     OnPropertyChanged(propertyName); 
     return true; 
    } 

    return false; 
} 

//.. 

protected void OnPropertyChanged(string propertyName) 
{ 
    try 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    catch (Exception ex) 
    { 

    } 
} 
+0

“SessionImage”的定义是什么? – entropic

+0

对不起,我错过了它的财产。 1秒。谢谢@entropic –

+0

如果您使用'image.CacheOption = BitmapCacheOption.None;'它是否正常工作?因此根本没有图像缓存?基于[MSDN](https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapcacheoption(v = vs.110).aspx),它看起来像“OnLoad”意思是它将缓存图像OnLoad,并重用缓存的值。 – Rachel

回答

2

WPF缓存是由URI的加载位图。为了避免这种情况,直接从文件加载BitmapImage代替:

using (var fileStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read)) 
{ 
    BitmapImage image = new BitmapImage(); 
    image.BeginInit(); 
    image.CacheOption = BitmapCacheOption.OnLoad; 
    image.StreamSource = fileStream; 
    image.EndInit(); 
    return image; 
} 
+0

就是这样!谢谢。工作)) –

相关问题