如何将结果返回到 Windows Phone 中的 UI 线程?
有谁知道如何将结果返回到 UI 线程的好主意? 我写了这段代码,但是它会编译错误,因为它不能以异步方式返回“img”。
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'
}
Does anyone know great idea How to return result to UI thread ?
I wrote this code, but It will be compile error because it can't return "img" in async.
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'
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的方法更改为:
用法:
Or (没有 lambda 表达式的旧式代码):
Change your method to:
Usage:
Or (old-style code without lambda expressions):