开发者

Creating BitmapImage on background thread WP7

开发者 https://www.devze.com 2023-03-16 07:32 出处:网络
I\'m receiving an UnauthorizedAccessException (\"Invalid cross-thread access.\") when running the following code on a background (threadpool) thread, is this expected behaviour?

I'm receiving an UnauthorizedAccessException ("Invalid cross-thread access.") when running the following code on a background (threadpool) thread, is this expected behaviour?

 var uri = new开发者_如何学运维 Uri("resourcevault/images/defaultSearch.png", UriKind.Relative);
 var info = Application.GetResourceStream(uri);

 // this line throws exception....
 this.defaultSearchImage = new BitmapImage();


The reason is because your background thread cannot directly be used to update the UI. Instead, you need to use a Dispatcher to marshal the data on to the UI thread. Something like this:

var uri = new Uri("resourcevault/images/defaultSearch.png", UriKind.Relative);
var info = Application.GetResourceStream(uri);

Dispatcher.BeginInvoke(() => {        
    this.defaultSearchImage = new BitmapImage();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号