1
當
我試圖使用異步調用像這樣從Internet下載圖像:Windows Phone的C#,UnauthorizedAccessException創建的BitmapImage
private void DoGetAlbumart(object sender, DoWorkEventArgs e)
{
string req = (string)e.Argument;
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(ReadWebRequestCallback);
wc.OpenReadAsync(new Uri(req));
}
void ReadWebRequestCallback(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
try
{
BitmapImage image = new BitmapImage();
image.SetSource(e.Result);
SecondTile.Source = image;
}
catch (Exception ex)
{
}
}
else
{
}
}
看來,當在BitmapImage的圖像=新的BitmapImage()的斷點,我得到了以下例外:
ex = {System.UnauthorizedAccessException:無效的跨線程訪問。 在MS.Internal.XcpImports.CheckThread() 在System.Windows.DependencyObject..ctor(UInt32的nativeTypeIndex,IntPtr的constructDO) 在System.Windows.Media.Imaging.BitmapImage..ctor()
還有什麼我可以嘗試擺脫這個錯誤嗎?