Pergunta

I am working on binding a URI to an image and I have binding working just fine. The problem is that the images are generated on the fly and the generation could throw an exception. I'm using a converter but I can't seem to attach to the DecodeFailed event properly, either that or there's something else I'm missing. Here is my code:

Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim image As New System.Windows.Media.Imaging.BitmapImage()
        Try
            image = New System.Windows.Media.Imaging.BitmapImage(New Uri(CStr(value)))
            AddHandler image.DownloadFailed, AddressOf DecodeFailed
            AddHandler image.DecodeFailed, AddressOf DecodeFailed
        Catch
            Return Windows.DependencyProperty.UnsetValue
        End Try
        Return image
    End Function

    Private Sub DecodeFailed(ByVal sender As Object, ByVal e As System.Windows.Media.ExceptionEventArgs)
        Dim image As System.Windows.Media.Imaging.BitmapImage = DirectCast(sender, System.Windows.Media.Imaging.BitmapImage)
        image.UriSource = New Uri("C:\Users\myUser\Desktop\errorSign.jpg")
    End Sub

Neither the DecodeFailed or DownloadFailed are firing when I throw an exception from the download handler. The Coverter is definitely in use and images are displaying.

...
<myNs:ImageConverter x:Key="ImageConverter"></myNs:ImageConverter>
...
<Image Height="125" Source="{Binding Path=Uri, Converter={StaticResource ImageConverter}}"/>
Foi útil?

Solução

Figured it out:

Public Shared Event DownloadOrDecodeFailed(ByVal oldUri As String, ByVal newUri As String)
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
            Dim image As New System.Windows.Media.Imaging.BitmapImage()
            Try
                AddHandler image.DownloadFailed, AddressOf OnDownloadOrDecodeFailed
                AddHandler image.DecodeFailed, AddressOf OnDownloadOrDecodeFailed
                image.BeginInit()
                image.UriSource = DirectCast(value, Uri)
                image.EndInit()
            Catch
                Return Windows.DependencyProperty.UnsetValue
            End Try
            Return image
        End Function

        Private Shared Sub OnDownloadOrDecodeFailed(ByVal sender As Object, ByVal e As System.Windows.Media.ExceptionEventArgs)
            Dim image As System.Windows.Media.Imaging.BitmapImage = DirectCast(sender, System.Windows.Media.Imaging.BitmapImage)
            RaiseEvent DownloadOrDecodeFailed(image.UriSource.AbsolutePath, "C:\Users\myUser\Desktop\errorSign.jpg")
        End Sub

Because the event is shared, I can attach to it and update the image uri from the view model if the image source that threw the event is the same.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top