1

In windows phone 7 I'm doing a simple async lookup to find an image by uri and set the returned binary as the source for an image control.

public object SetImageFromUri(string uri)
{
    var wc = new WebClient();
    wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
    wc.OpenReadAsync(new Uri(uri), wc);

    return null;
}

void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    if (e.Error == null && !e.Cancelled)
    {
            var image = new BitmapImage();
            image.SetSource(e.Result);

            //e.Result has a property in the memory stream labeled finalUri
            //someImageControl.Source = image;

    }
}

My question is- how can I pull out the final uri property from the e.Result so I can see what image control it's associated with

Thank you in advance

Toran Billups
  • 27,111
  • 40
  • 155
  • 268

3 Answers3

2

Instead of passing the WebClient through as the second parameter, pass the Uri (or some other piece of usefule state information)

wc.OpenReadAsync(new Uri(uri), uri);

You can then access this in your callback

var uri = (string)e.UserState;
Chris Sainty
  • 9,086
  • 1
  • 26
  • 31
1

Due to specific restrictions implemented in the Reflection mechanism, you cannot access internal content from sandboxed code. Ultimately, you would want to use something like this:

FieldInfo f = e.Result.GetType().GetField("_finalUri", BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance);
Uri n = (Uri)f.GetValue(e.Result);

However, this will cause a FieldAccessException. If you are not using a redirect URI, then you can simply reuse the parameter that is initially passed to your method. If not, you need to check HttpWebRequest and follow the idea I outlined a couple of days ago.

Community
  • 1
  • 1
Den
  • 16,686
  • 4
  • 47
  • 87
1

You could also just bind directly to the Image, and use the LowProfileImageLoader, to avoid it blocking the UI thread during the load. (Remember to set a FallBack image)

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150