0
有誰知道好主意如何將結果返回給UI線程? 我寫了這段代碼,但它會編譯錯誤,因爲它不能在異步中返回「img」。如何將結果返回到Windows Phone中的UI線程?
public byte[] DownloadAsync2(Uri address)
{
byte[] img;
byte[] buffer = new byte[4096];
var wc = new WebClient();
wc.OpenReadCompleted += ((sender, e) =>
{
using (MemoryStream memoryStream = new MemoryStream())
{
int count = 0;
do
{
count = e.Result.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
} while (count != 0);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (e.Error == null) img = memoryStream.ToArray();
});
}
}
);
wc.OpenReadAsync(address);
return img; //error : Use of unassigned local variable 'img'
}
感謝您的回覆。我不熟悉Action。你會告訴我如何在使用中使用Action嗎? –
2012-02-23 14:07:52
我想你的代碼中有一個小姐。 「if(e.Error!= null)」應該是「if(e.Error == null)」。我修好了,我確認了它的工作。 – 2012-02-24 16:26:40
是的,謝謝.... – Ku6opr 2012-02-24 18:47:35