Creating BitmapImage on background thread WP7

≡放荡痞女 提交于 2020-01-02 05:19:08

问题


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();

回答1:


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();
});


来源:https://stackoverflow.com/questions/6518783/creating-bitmapimage-on-background-thread-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!