2014-10-10 72 views
1

我這是從這裏創建一個DataTemplate: ComboBoxes sharing Observable Collection keeps breaking顯示的圖像鎖定源文件

<UserControl.Resources> 
    <DataTemplate x:Key="ImageItemTemplate"> 
     <StackPanel Orientation="Horizontal"> 
      <Image Height="44" Source="{Binding Path}"/> 
      <Label Content="{Binding Name}" VerticalAlignment="Center"/> 
     </StackPanel> 
    </DataTemplate> 
</UserControl.Resources> 

<ComboBox x:Name="image1" ItemTemplate="{StaticResource ImageItemTemplate}"/> 

代碼:

public ObservableCollection<ImageItem> images = new ObservableCollection<ImageItem>(); 

Generic.ImportGrfx(tabID, image1, images); 

public static void ImportGrfx(string tabID, ComboBox combo, ObservableCollection<ImageItem> items) 
{ 
    items.Clear(); 

    try 
    { 
     string root = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); 
     var files = Directory.GetFiles(Path.Combine(root, "input\\" + tabID), "*.png"); 

     foreach (var file in files) 
     { 
      ImageItem item = new ImageItem(); 
      item.Path = file; 
      item.Name = Path.GetFileName(file).Remove(Path.GetFileName(file).Length - 4); 

      items.Add(item); 
     } 
    } 
    catch (Exception) { } 

    combo.ItemsSource = items; 
} 

public class ImageItem 
{ 
    public string Path { get; set; } 
    public string Name { get; set; } 
} 

我有約束力這些圖像的問題數據模板「鎖定」圖像源。含義在程序運行時我無法編輯圖像...我會看到一個錯誤,指出圖像正在使用中。有沒有辦法來解決這個問題?

回答

1

下面是一個Image的框架,每當您從外部修改它時都會進行更新。

  • 您設置源,它會複製到一個臨時通道,並從那裏加載圖像
  • 其手錶在初始圖像和沖洗,重複的變化再次更新自己
  • :d

如果您只需要能夠從外部編輯圖像,則可以使用自動更新功能,而不是

這是很基本的可以隨意提高...

enter image description here

代碼:

using System; 
using System.IO; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media.Imaging; 

namespace WpfApplication4 
{ 
    /// <summary> 
    ///  Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      Loaded += MainWindow_Loaded; 
     } 


     private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
     { 
      DynamicImage1.SetSource(@"d:\Untitled.png"); 
     } 
    } 

    internal class DynamicImage : Image 
    { 
     private string _name; 
     private FileSystemWatcher _watcher; 

     public void SetSource(string fileName) 
     { 
      if (fileName == null) throw new ArgumentNullException("fileName"); 

      if (_watcher != null) 
      { 
       _watcher.Changed -= watcher_Changed; 
       _watcher.Dispose(); 
      } 
      string path = Path.GetDirectoryName(fileName); 
      _watcher = new FileSystemWatcher(path); 
      _watcher.EnableRaisingEvents = true; 
      _watcher.Changed += watcher_Changed; 
      _name = fileName; 
      string tempFileName = Path.GetTempFileName(); 
      File.Copy(fileName, tempFileName, true); 
      Source = new BitmapImage(new Uri(tempFileName)); 
     } 

     private void watcher_Changed(object sender, FileSystemEventArgs e) 
     { 
      bool b = string.Equals(e.FullPath, _name, StringComparison.InvariantCultureIgnoreCase); 
      if (b) 
      { 
       string tempFileName = Path.GetTempFileName(); 
       File.Copy(e.FullPath, tempFileName, true); 

       Dispatcher.BeginInvoke((Action) (() => { Source = new BitmapImage(new Uri(tempFileName)); })); 
       _name = e.FullPath; 
      } 
     } 
    } 
} 

XAML:

<Window x:Class="WpfApplication4.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:wpfApplication4="clr-namespace:WpfApplication4" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <wpfApplication4:DynamicImage x:Name="DynamicImage1" /> 

    </Grid> 
</Window> 
+1

我不需要它來監控圖像變化。實際上我的程序中有一個按鈕用於重新加載圖像列表。但是你的代碼確實爲我提供了我需要的東西......'''''''' File.Copy(e.FullPath,tempFileName,true);'。謝了哥們! – 2014-10-10 21:57:51

+0

很高興幫助你:D – Aybe 2014-10-10 21:58:37