I have a Windows 8 store app in XAML. I have an Image control that binds to a property, but because the URL I want to display requires authentication, I have to create a webresponse and generate the bitmap that way instead of just providing a URL to my image control.
Problem is, the memorystream operations are async, and properties on an object have to be sync, not async. So I have a pretty simple setup:
public ImageSource ImageSource
{
get { return Task.Run(() => BitmapImageUtils.ToImage(this.Upload.ThumbFile)).Result; }
and the Image control has the ImageSource property as its binding. Problem is, I'm receiving the below exception. there's multiple Image controls in a ListView and they're all binding in this way, and my guess is that the UI thread that invokes this somehow hands off control to a thread then tries to come back somehow. I'm a little new to this.
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
Any help is appreciated.
--- EDIT ToImage Method
public static async Task<BitmapImage> ToImage(byte[] byteArray)
{
var bitmapImage = new BitmapImage();
var stream = new InMemoryRandomAccessStream();
await stream.WriteAsync(byteArray.AsBuffer());
stream.Seek(0);
await bitmapImage.SetSourceAsync(stream);
return bitmapImage;
}
Exception (Note the exception is an inner exception of an aggregate exception, which I believe is pretty standard for async/await exceptions
at Windows.UI.Xaml.Media.Imaging.BitmapImage..ctor()
at Campfire.Utils.BitmapImageUtils.<ToImage>d__0.MoveNext()