在 Silverlight WP7 中伪造同步调用 [英] Faking synchronous calls in Silverlight WP7

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

问题描述

我正在将一些代码从完整的 .NET 框架移植到 WP7 版本,但我遇到了同步调用与异步调用的问题.

I'm porting some code from the full .NET framework to the WP7 version and I'm running into an issue with synchronous vs async calls.

 string response;
 string requestString = GenerateReqString();
 HttpWebRequest req = (HttpWebRequest) WebRequest.Create("endpoint");
 req.Method = "POST";
 req.ContentType = "text/xml";

 req.ContentLength = requestString.Length;

 StreamWriter sw = new StreamWriter (req.GetRequestStream(), System.Text.Encoding.ASCII);
 sw.Write(requestString);
 sw.Close();

 StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream());
 response = sr.ReadToEnd();
 sr.Close();

然后将响应字符串解析为该方法返回的对象列表.

The response string is then parsed into a list of objects that is returned by the method.

我遇到的问题是没有办法在 Silverlight/WP7 中同步进行调用.如果我使用回调,我将在不同的函数中获得响应,并且无法从原始函数返回它.有没有办法同步调用或从 CallBack 函数返回到启动异步调用的方法?

The problem I'm having is that there isn't a way to make the call synchronously in Silverlight/WP7. If I use a call back I'll get the response in a different function and wont be able to return it from the original function. Is there a way to either make the call synchronously or return from the CallBack function back to the method that kicked off the async call?

推荐答案

您需要以不同的方式思考问题.要使异步事物感觉"同步,最简单的方法是重构代码以利用 '继续传递风格'.

You need to think about the problem differently. To make asynchronous things "feel" synchronous, the easiest way to do it is to restructure your code to make use of 'continuation passing style'.

本质上,不是调用返回值的函数然后处理该值,而是调用函数,将匿名函数作为委托传递给它.然后被调用的函数将调用委托,传入字符串.

In essence, instead of calling a function that returns a value and then you process that value, you call a function, passing an anonymous function as a delegate to it. The called function will then invoke the delegate, passing in the string.

这是一个使用匿名函数和 lambda 表达式的示例:

Here is an example, which uses anonymous functions and lambdas:

void DoSomethingAsync( Action<string> callback ) {
    HttpWebRequest req; // TODO: build your request

    req.BeginGetResponse( result => {
        // This anonymous function is a closure and has access 
        // to the containing (or enclosing) function.
        var response = req.EndGetResponse( result );

        // Get the result string and call the callback
        string resultString = null; // TODO: read from the stream

        callback(resultString);
    }, null );
}

这是解决方案的一半.下一部分是实际调用它.想象一下,您有一个 ICommand 实例或更简单的按钮单击事件,需要调用此函数并获取字符串".你调用这个函数并提供一个回调方法(这将是一个闭包)而不是获取字符串".

This is one half the solution. The next part, is to actually call this. Imagine you have an ICommand instance or simpler, a button click event that needed to call this function and "get the string". Instead of "getting the string" you call this function and supply a callback method (which will be a closure).

void btnGo_Click( object sender, EventArgs e ) {
    DoSomethingAsync( resultString => {
        // This anonymous function is called when the web request has
        // finished and has your string. 

        // Now that we have the string, we can go and process it.
        ProcessWebResponseResult( resultString );
    });
}

这是一篇非常好的文章,进一步解释了这个概念:http://blogs.msdn.com/b/wesdyer/archive/2007/12/22/continuation-passing-style.aspx

Here is a really good article explaining the concept further: http://blogs.msdn.com/b/wesdyer/archive/2007/12/22/continuation-passing-style.aspx

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

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