BitmapImage ImageOpened event does not fire on background agent

As in the header, the event does not fire when the code is executed in the BackgroundAgent , while it works fine, when I execute it in the main application.

Here is 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 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 runs on a Dispatcher stream, and I'm trying to load a remote image (I cannot cache it because it is a dynamic image generated on a php page).

Any clues?

EDIT:

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

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

still no success.

+7
c # events windows-phone-8 background-agents
source share
1 answer

I can only assume that it loads the image so fast that you register the event handler too quickly. Try to instantiate BitmapImage first and then set uri

+1
source share

All Articles