Silverlight 中的同步 WebClient 下载

发布于 2024-10-26 21:35:36 字数 205 浏览 1 评论 0原文

我需要下载一个字符串(特别是来自 PHP Web 服务的 JSON 数组结果),作为返回字符串的函数,而不是 DownloadStringAsync。我需要这个,因为我正在编写一个下载字符串的函数,然后将其转换为 JsonArray。

我正在使用 Visual Studio Ultimate 2010,我正在开发 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. I need this because I am writing a function that downloads the string, and then converts it to a JsonArray.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖伴 2024-11-02 21:35:36

您正在寻找的不是同步工作流程(这在 Silverlight 中即使不是不可能,也是非常困难的)。相反,您希望能够按顺序管理异步工作流程。您希望能够说“从 Web 服务下载此字符串,然后将该字符串转换为 JSON 数组”,而无需处理回调和事件的混乱。

嗯,有好消息和坏消息。好消息是这个问题有一个解决方案 - 它称为协程。协程是一种停止执行连续代码片段直到最后一部分完成的方法,即使该部分是异步的。

坏消息是,协程并不是在 C# 中原生实现的(尽管它们是 在 C# 中实现的5)。您可以实现自己的顺序工作流程,并且有一篇关于它的绝对精彩的文章

但不要绝望!有一个更简单的方法。 Caliburn.Micro 是一个 MVVM 框架,实际上有一个简单的协程实现。事实上,如果您确实愿意,您可以非常轻松地使用 Caliburn.Micro 协程,而无需使用框架的任何其他部分。 Caliburn.Micro 的创建者 Rob Eisenberg 有一篇关于协程的优秀文章,包括理论和实践,此处

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

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
}

至少我认为这就是你正在寻找的:)

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.

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.

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 :)

↘人皮目录ツ 2024-11-02 21:35:36

或者,如果您使用 MVVM Light,Matt Hamilton 为该框架创建了协程:http://matthamilton。净/协程-with-mvvm-light

Or if you're using MVVM Light, Matt Hamilton created coroutines for that framework: http://matthamilton.net/coroutines-with-mvvm-light

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文