我刚开始学习编程的WP所以这可能是有点愚蠢的问题...
I have just started learning WP programming so this might be little silly question...
林开发应用程序,它从一个Web服务的一些不同的方法获取数据。所以我决定把所有的Web服务获取code到一个类:
Im developing app which fetch data from one web services few different method. So I decided to put all this web service fetching code to one class:
class WebApiWorker { public async Task<List<GeocodeResponse>> GetGeocodeAsync(String address) { String url = "api&search=" + HttpUtility.UrlEncode(address) + "&format=json"; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = "GET"; HttpWebResponse response = (HttpWebResponse) await httpWebRequest.GetResponseAsync(); Stream responseStream = response.GetResponseStream(); string data; using (var reader = new System.IO.StreamReader(responseStream)) { data = reader.ReadToEnd(); } responseStream.Close(); var geocodeResponse = JsonConvert.DeserializeObject<List<GeocodeResponse>>(data); return geocodeResponse; } }不过,我应该如何从我的主要的应用程序code调用这个,我尝试这样的:
But how I should call this from my "main app" code, im trying something like this:
WebApiWorker webApi = new WebApiWorker(); var geoResponse = await webApi.GetGeocodeAsync("address");所以什么错,我得到的编译器错误:在'等待'运算符只能异步方法中使用。考虑标志着该方法与异步修饰和改变它的返回类型为'任务'。
So whats wrong with this, i get the compiler error: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
所有建议都欢迎。
推荐答案请确保你的方法在你的主应用程序code也有异步修改
Make sure your method in your "main app" code also has the "async" modifier:
public async Task GetAddress() { WebApiWorker webApi = new WebApiWorker(); var geoResponse = await webApi.GetGeocodeAsync("address"); }更多推荐
的Windows Phone 8异步等待用法
发布评论