在后台(线程池)线程上运行以下代码时,我收到 UnauthorizedAccessException(“无效的跨线程访问。”),这是预期的行为吗?
var uri = new Uri("resourcevault/images/defaultSearch.png", UriKind.Relative);
var info = Application.GetResourceStream(uri);
// this line throws exception....
this.defaultSearchImage = new BitmapImage();
最佳答案
原因是因为你的后台线程不能直接用来更新UI。相反,您需要使用 Dispatcher
将数据编码到 UI 线程。像这样:
var uri = new Uri("resourcevault/images/defaultSearch.png", UriKind.Relative);
var info = Application.GetResourceStream(uri);
Dispatcher.BeginInvoke(() => {
this.defaultSearchImage = new BitmapImage();
});
关于c# - 在后台线程 WP7 上创建 BitmapImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6518783/