Silverlight中的同步WebClient下载 [英] Synchronous WebClient Download In Silverlight

查看:127
本文介绍了Silverlight中的同步WebClient下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你下载一个字符串(尤其是从一个PHP Web服务的JSON数组结果),作为一个返回字符串,而不是DownloadStringAsync功能。我需要这个,因为我写了下载的字符串,然后将其转换为JsonArray功能。

I need do download a string (specifically the JSON array result from a PHP webservice), as a function that returns a string, not DownloadStringAsync. I need this because I am writing a function that downloads the string, and then converts it to a JsonArray.

我使用Visual Studio的终极2010年,我正在开发一个Silverlight应用程序,任何帮助将不胜感激。

I am using Visual Studio Ultimate 2010, I am developing a Silverlight application, and any help will be appreciated.

推荐答案

什么是你要找的不同步工作流程(这将是非常困难,如果不是不可能的,在Silverlight)。而要能够管理的异步的工作流程顺序的。你要能够说下载从Web服务这个字符串,然后将字符串转换为JSON数组,没有处理回调和事件的混乱。

What you're looking for is not synchronous workflows (this would be very difficult, if not impossible, in Silverlight). Rather you want to be able to manage asynchronous workflows sequentially. You want to be able to say "Download this string from the web service, and then convert the string to a JSON array", without the messiness of handling callbacks and events.

那么有好消息和坏消息。好消息是,有这个问题的解决方案 - 这就是所谓的协同程序。协同程序,直到最后部分已经完成,即使该部分是异步停止顺序一段代码的执行方式。

Well there is good news and bad news. The good news is that there is a solution to this problem - it's called Coroutines. Coroutines are a way of halting execution of a sequential piece of code until the last part has completed, even if that part is asynchronous.

坏消息是,协程不在C#中本地实现的(尽管它们是未来在C#5 )。您可以实现自己的连续工作流程,并有一个关于它的绝对精彩文章的此处。这是一个很长的文章,它是如果你从来没有这样做有点困难。

The bad news is that coroutines are not natively implemented in C# (although they are coming in C# 5). You can implement your own sequential workflows, and there is an absolutely brilliant article about it here. It's a long article and it's a little difficult if you've never done it before.

但不绝望!还有一个更简单的方法。 Caliburn.Micro 是一个MVVM框架,实际上有一个简单的协同程序的实现。事实上,你可以很容易使用Caliburn.Micro协同程序不使用框架的任何其他部分,如果你真的想。 Caliburn.Micro的创造者,抢艾森伯格,大约有协同程序的优秀文章,包括理论和实践的此处

But despair not! There is a simpler way. Caliburn.Micro is an MVVM framework that actually has a simple coroutine implementation. In fact, you could quite easily use the Caliburn.Micro coroutines without using any other part of the framework, if you really want to. The creator of Caliburn.Micro, Rob Eisenberg, has an excellent article about coroutines, including theory and practice, here.

基本上,你的代码最终会看起来像这样:

Basically your code will end up looking something like this:

public IEnumerable<IResult> DoTheThing() {
  var json = new FetchString("webserviceaddress.asmx");
  yield return json;
  var jsonStr = json.Result;
  var jsonArray = createJsonArray(jsonStr);
  // do stuff with the array
}



至少我认为这是)

At least I think that's what you're looking for :)

这篇关于Silverlight中的同步WebClient下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆