ObservableCollection FileSystemWatcher ListBox更新问题

时间:2009-08-01 03:56:02

标签: .net wpf data-binding observablecollection filesystemwatcher

我有一个ObservableCollection,它使用FileSystemWatcher自动添加已添加到目录中的其他PNG图像。 ListBox使用以下XAML将PhotosSource数据绑定到Photos对象。

<ListBox ItemsSource="{Binding Source={StaticResource Photos}}" IsSynchronizedWithCurrentItem="True"/>

但是,当将PNG文件添加到受监视目录时,正在调用OnPhotoCreated事件(断点确认此情况),但ListBox UI未更新。有什么想法吗?

Public Class Photos
Inherits Collections.ObjectModel.ObservableCollection(Of BitmapImage)

' Events
Public Event ItemsUpdated As EventHandler

' Fields
Private FileSystemWatchers As Dictionary(Of String, FileSystemWatcher) = New Dictionary(Of String, FileSystemWatcher)

' Methods
Protected Overrides Sub ClearItems()
    MyBase.ClearItems()
    Me.FileSystemWatchers.Clear()
End Sub

Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As BitmapImage)
    MyBase.InsertItem(index, item)
    Dim ImagePath As String = IO.Path.GetDirectoryName(item.UriSource.LocalPath)
    If Not Me.FileSystemWatchers.ContainsKey(ImagePath) Then
        Dim FileWatcher As New FileSystemWatcher(ImagePath, "*.png")
        FileWatcher.EnableRaisingEvents = True
        AddHandler FileWatcher.Created, New FileSystemEventHandler(AddressOf Me.OnPhotoCreated)
        AddHandler FileWatcher.Deleted, New FileSystemEventHandler(AddressOf Me.OnPhotoDeleted)
        AddHandler FileWatcher.Renamed, New RenamedEventHandler(AddressOf Me.OnPhotoRenamed)
        Me.FileSystemWatchers.Add(ImagePath, FileWatcher)
    End If
End Sub

Private Sub OnPhotoCreated(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    MyBase.Items.Add(New BitmapImage(New Uri(e.FullPath)))
    RaiseEvent ItemsUpdated(Me, New EventArgs)
End Sub

Private Sub OnPhotoDeleted(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    Dim index As Integer = -1
    Dim i As Integer
    For i = 0 To MyBase.Items.Count - 1
        If (MyBase.Items.Item(i).UriSource.AbsolutePath = e.FullPath) Then
            index = i
            Exit For
        End If
    Next i
    If (index >= 0) Then
        MyBase.Items.RemoveAt(index)
    End If
    RaiseEvent ItemsUpdated(Me, New EventArgs)
End Sub

Private Sub OnPhotoRenamed(ByVal sender As Object, ByVal e As RenamedEventArgs)
    Dim index As Integer = -1
    Dim i As Integer
    For i = 0 To MyBase.Items.Count - 1
        If (MyBase.Items.Item(i).UriSource.AbsolutePath = e.OldFullPath) Then
            index = i
            Exit For
        End If
    Next i
    If (index >= 0) Then
        MyBase.Items.Item(index) = New BitmapImage(New Uri(e.FullPath))
    End If
    RaiseEvent ItemsUpdated(Me, New EventArgs)
End Sub


End Class

更新#1:我尝试过如下所示的活动。这导致InvalidOperationException崩溃,“调用线程无法访问此对象,因为另一个线程拥有它”当尝试滚动到视图中时,新图像。我希望不需要刷新方法。

Dim Photos As Photos = CType(Me.FindResource("Photos"), Photos)
AddHandler Photos.ItemsUpdated, AddressOf Me.Photos_ItemsUpdated

Private Sub RefreshPhotos()
    '
    If Me.ImageListBox.Dispatcher.CheckAccess = True Then
        Me.ImageListBox.Items.Refresh()
    Else
        Dispatcher.BeginInvoke(DispatcherPriority.Normal, New DispatcherMethodCallback(AddressOf Me.RefreshPhotos))
    End If
    '
End Sub

Private Sub Photos_ItemsUpdated(ByVal sender As Object, ByVal e As EventArgs)
    '
    Debug.WriteLine("PhotosUpdated")
    Me.RefreshPhotos()
    '
End Sub

1 个答案:

答案 0 :(得分:1)

查看ObservableCollection类(来自Reflector)的InsertItem代码:

Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As T)
    Me.CheckReentrancy
    MyBase.InsertItem(index, item)
    Me.OnPropertyChanged("Count")
    Me.OnPropertyChanged("Item[]")
    Me.OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index)
End Sub

现在使用它作为指导ObservableCollection应该执行的通知。 OnCollectionChanged方法是wpf通知系统的主要连接,如果它被适当调用,那么这个calss工作正常,其他地方就是问题。

此外,在您的OnPhotoCreated方法中,您调用{strong>不执行通知的Items.Add方法,它不属于ObservableCollection类,而是属于{{1} ObservableCollection继承的类。

对于InvalidOperationException错误,它听起来像是从错误的线程向我更新UI的情况。