Web services Windows Phone 7:web服务问题

Web services Windows Phone 7:web服务问题,web-services,asynchronous,windows-phone-7,Web Services,Asynchronous,Windows Phone 7,在WindowsPhone7应用程序中,我需要通过发送一个“category”字符串参数来查询web服务,并期望得到一个字符串作为返回。我试图按照MSDN中相同的“天气预报”示例进行操作,但总是得到一个空字符串。如果我添加一些Debug.WriteLine命令,我可以看到回调在返回响应后执行,即:返回不会等待异步操作结束 我怎么会100%尊重示例代码,有人能看出哪里出了问题吗?代码如下,感谢您抽出时间: public partial class MainPage : PhoneAppli

在WindowsPhone7应用程序中,我需要通过发送一个“category”字符串参数来查询web服务,并期望得到一个字符串作为返回。我试图按照MSDN中相同的“天气预报”示例进行操作,但总是得到一个空字符串。如果我添加一些Debug.WriteLine命令,我可以看到回调在返回响应后执行,即:返回不会等待异步操作结束

我怎么会100%尊重示例代码,有人能看出哪里出了问题吗?代码如下,感谢您抽出时间:

    public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Event handler to handle when this page is navigated to
    /// </summary>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        SearchService searchService = new SearchService();
        searchService.GetCategoryCounter("pizza")
        Globals.pizzaCounter = searchService.Counter;    

        searchService.GetCategoryCounter("pasta")
        Globals.pastaCounter = searchService.Counter;

        pizzacounter.Text = Globals.pizzaCounter;
        pastacounter.Text = Globals.pastaCounter;
    }
}
public部分类主页:PhoneApplicationPage
{
//建造师
公共主页()
{
初始化组件();
}
/// 
///导航到此页时要处理的事件处理程序
/// 
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
SearchService SearchService=新的SearchService();
searchService.GetCategoryCounter(“比萨饼”)
Globals.pizzaccounter=searchService.Counter;
searchService.GetCategoryCounter(“意大利面”)
Globals.pastaCounter=searchService.Counter;
pizzaccounter.Text=Globals.pizzaccounter;
Text=Globals.pastacounter;
}
}

公共类搜索服务
{
#区域成员变量
私有字符串currentCategoryCount=“”;
公共事件属性更改事件处理程序属性更改;
#端域成员变量
#区域存取器
公共字符串计数器
{
得到
{
返回当前类别计数;
}
设置
{
如果(值!=currentCategoryCount)
{
currentCategoryCount=值;
NotifyPropertyChanged(“计数器”);
}
}
}
#端域存取器
#地区私人助理
/// 
///引发PropertyChanged事件并传递已更改的属性
/// 
私有void NotifyPropertyChanged(字符串属性)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(property));
}
}
#终端区域私人助理
/*
*GetCategoryCounter方法:
* 
*@param类别
*:要检索其计数器的类别
*/
public void GetCategoryCounter(字符串类别)
{
尝试
{
//形成URI
UriBuilder fullUri=新的UriBuilder(“http://www.mywebsite.com/items/stats");
fullUri.Query=“category=”+类别;
//初始化新的WebRequest
HttpWebRequest counterRequest=(HttpWebRequest)WebRequest.Create(fullUri.Uri);
//为异步请求设置状态对象
CounterUpdateState counterState=新CounterUpdateState();
counterState.AsyncRequest=反请求;
//启动异步请求
counterRequest.BeginGetResponse(新的AsyncCallback(HandleCounterResponse),counterState);
返回;
}
捕获(例外情况除外)
{
掷骰子;
}
}
/// 
///处理从异步请求返回的信息
/// 
/// 
私有无效HandleCounterResponse(IAsyncResult asyncResult)
{
//获取状态信息
CounterUpdateState counterState=(CounterUpdateState)asyncResult.AsyncState;
HttpWebRequest counterRequest=(HttpWebRequest)counterState.AsyncRequest;
//结束异步请求
counterState.AsyncResponse=(HttpWebResponse)counterRequest.EndGetResponse(asyncResult);
溪流结果;
字符串currentCount=“”;
尝试
{
//从异步调用获取包含响应的流
streamResult=counterState.AsyncResponse.GetResponseStream();
StreamReader reader=新的StreamReader(streamResult);
currentCount=reader.ReadToEnd();
//把数据复制过来
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
计数器=当前计数;
});
}
捕获(格式化异常)
{
掷骰子;
}
}
}
/// 
///BeginGetResponse异步调用的状态信息
/// 
公共类房地产
{
公共HttpWebRequest异步请求{get;set;}
公共HttpWebResponse异步响应{get;set;}
}

您的示例代码甚至无法编译。。。比较一下:

Globals.pizzaCounter = searchService.GetCategoryCounter("pizza");
为此:

public void GetCategoryCounter(string category)
请注意它是一个void方法,因此不能将返回值分配给
Globals.pizzaccounter

您需要记住,这是异步发生的。正如您所说,“如果我添加一些Debug.WriteLine命令,我可以看到回调在返回响应后执行,即:返回不会等待异步操作结束。”这就是异步的全部含义。您不想阻止等待服务返回的UI线程

相反,您应该向服务获取代码传递一个回调,以便在服务返回一些数据时执行。然后需要通过
调度程序
封送回UI线程,然后它可以更新文本框。您的代码已经完成了一些操作,方法是将数据编组回UI线程,然后更新属性,这将引发
PropertyChanged
事件,让UI知道如何从属性中重新提取数据。然而,看起来只有一个计数器,而您试图获取两个不同的类别。您可能应该将服务本身与“类别计数器”(或其他任何东西)分开,后者了解搜索服务,但也特定于一个类别。您将有两个这样的实例(但只有一个搜索服务);每个文本框都将绑定到类别计数器的相关实例。

My bad,我尝试了sev
public void GetCategoryCounter(string category)