简体   繁体   English

在Silverlight中下载同步WebClient

[英]Synchronous WebClient Download In Silverlight

I need do download a string (specifically the JSON array result from a PHP webservice), as a function that returns a string, not DownloadStringAsync. 我需要下载一个字符串(特别是来自PHP Web服务的JSON数组结果),作为返回字符串而不是DownloadStringAsync的函数。 I need this because I am writing a function that downloads the string, and then converts it to a JsonArray. 我需要这个,因为我正在编写一个下载字符串的函数,然后将其转换为JsonArray。

I am using Visual Studio Ultimate 2010, I am developing a Silverlight application, and any help will be appreciated. 我正在使用Visual Studio Ultimate 2010,正在开发Silverlight应用程序,我们将不胜感激。

What you're looking for is not synchronous workflows (this would be very difficult, if not impossible, in Silverlight). 您要找的不是同步工作流(在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. 您希望能够说“从Web服务下载此字符串,然后将字符串转换为JSON数组”,而不会处理回调和事件。

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 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. 协程是一种方法,它可以停止执行一段连续的代码,直到最后一部分完成为止,即使该部分是异步的也是如此。

The bad news is that coroutines are not natively implemented in C# (although they are coming in C# 5 ). 坏消息是协程不是在C#中本地实现的(尽管它们是在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. 这是一篇很长的文章,如果您以前从未做过,那会有些困难。

But despair not! 但是绝望没有! There is a simpler way. 有一种更简单的方法。 Caliburn.Micro is an MVVM framework that actually has a simple coroutine implementation. Caliburn.Micro是一个MVVM框架,实际上具有简单的协程实现。 In fact, you could quite easily use the Caliburn.Micro coroutines without using any other part of the framework, if you really want to. 实际上,如果确实愿意,您可以很容易地使用Caliburn.Micro协程,而无需使用框架的任何其他部分。 The creator of Caliburn.Micro, Rob Eisenberg, has an excellent article about coroutines, including theory and practice, here . Caliburn.Micro的创建者Rob Eisenberg 在此处撰写了一篇有关协程的出色文章,其中包括理论和实践。

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 :) 至少我认为这就是您要寻找的:)

或者,如果您使用的是MVVM Light,则Matt Hamilton为该框架创建了协程: http : //matthamilton.net/coroutines-with-mvvm-light

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM