7

As in the title, the event is not fired when the code is executed in the BackgroundAgent while it works fine when I execute it in my main app.

Here's my code:

var background = new BitmapImage(new Uri(uri), UriKind.Absolute)){ CreateOptions = BitmapCreateOptions.None };
background.ImageFailed += (s, e) =>
{
    Debug.WriteLine(e);
    // Nothing happens here so I guess that there's no error in the image loading
};
background.ImageOpened += (s, e) =>
{
    Debug.WriteLine("ImageOpened");
    // This line is never printed no the event is never thrown
};

Everthing is running in a Dispatcher thread, and I'm trying to load a remote image (I can't cache it because it's a dynamic image generated by a php page).

Any hint?

EDIT:

Here's the code based on @l3arnon's suggestion:

var backgroundImage = new BitmapImage { CreateOptions = BitmapCreateOptions.None };
backgroundImage.ImageOpened += (s, e) =>
{
                Debug.WriteLine("ImageOpened");
};
backgroundImage.UriSource = new Uri(uri);

still no success though.

StepTNT
  • 3,867
  • 7
  • 41
  • 82
  • Where are you registering an event handler to the ImageLoaded event? – i3arnon Dec 29 '13 at 15:02
  • Actually I was talking about the `ImageOpened` event, I just edited the title – StepTNT Dec 29 '13 at 15:07
  • I am trying to catch ImageFailed event and it is never firing for local files. I tried it with text file with `.gif` extension, tried with non existing path. Nothing. Could be that your's is not firing neither. – Jānis Jun 14 '16 at 15:09
  • Seeing the same issue with Silverlight currently, did you get anywhere with this? Intermittent issue with ImageFailed/ImageLoaded not firing – Duncan Watts Jan 12 '18 at 15:33

1 Answers1

1

My only guess it that it loads the image so fast that you register the event handler too fast. Try first creating the BitmapImage instance and then setting a uri

i3arnon
  • 113,022
  • 33
  • 324
  • 344