异步 – WP7中的异步调用
我今天一直在尝试使用WP7应用程序并且已经打了一针墙.
我喜欢在用户界面和主应用程序代码之间进行分离,但我已经碰壁了. 我已经成功实现了webclient请求并获得了结果,但由于调用是异步的,我不知道如何将此备份传递到UI级别.我似乎无法等待对完成或任何事情的回应. (这是我在我的网站上下载的xbox360Voice库:http://www.jamesstuddart.co.uk/Projects/ASP.Net/Xbox_Feeds/,我将其作为测试移植到WP7) 这是后端代码片段: internal const string BaseUrlFormat = "http://www.360voice.com/api/gamertag-profile.asp?tag={0}"; internal static string ResponseXml { get; set; } internal static WebClient Client = new WebClient(); public static XboxGamer? GetGamer(string gamerTag) { var url = string.Format(BaseUrlFormat,gamerTag); var response = GetResponse(url,null,null); return SerializeResponse(response); } internal static XboxGamer? SerializeResponse(string response) { if (string.IsNullOrEmpty(response)) { return null; } var tempGamer = new XboxGamer(); var gamer = (XboxGamer)SerializationMethods.Deserialize(tempGamer,response); return gamer; } internal static string GetResponse(string url,string userName,string password) { if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) { Client.Credentials = new NetworkCredential(userName,password); } try { Client.DownloadStringCompleted += ClientDownloadStringCompleted; Client.DownloadStringAsync(new Uri(url)); return ResponseXml; } catch (Exception ex) { return null; } } internal static void ClientDownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e) { if (e.Error == null) { ResponseXml = e.Result; } } 这是前端代码: public void GetGamerDetails() { var xboxManager = XboxFactory.GetXboxManager("DarkV1p3r"); var xboxGamer = xboxManager.GetGamer(); if (xboxGamer.HasValue) { var profile = xboxGamer.Value.Profile[0]; imgAvatar.Source = new BitmapImage(new Uri(profile.ProfilePictureMiniUrl)); txtUserName.Text = profile.GamerTag; txtGamerScore.Text = int.Parse(profile.GamerScore).ToString("G 0,000"); txtZone.Text = profile.PlayerZone; } else { txtUserName.Text = "Failed to load data"; } } 现在我明白我需要在ClientDownloadStringCompleted中放置一些内容,但我不确定是什么.
您遇到的问题是,只要在代码路径中引入异步操作,整个代码路径就需要变为异步.
>因为GetResponse调用DownloadStringAsync它必须变为异步,它不能返回一个字符串,它只能在回调时执行 这里有一些空气代码可以解释代码中的某些异步性. public static void GetGamer(string gamerTag,Action<XboxGamer?> completed) { var url = string.Format(BaseUrlFormat,gamerTag); var response = GetResponse(url,(response) => { completed(SerializeResponse(response)); }); } internal static string GetResponse(string url,string password,Action<string> completed) { WebClient client = new WebClient(); if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) { client.Credentials = new NetworkCredential(userName,password); } try { client.DownloadStringCompleted += (s,args) => { // Messy error handling needed here,out of scope completed(args.Result); }; client.DownloadStringAsync(new Uri(url)); } catch { completed(null); } } public void GetGamerDetails() { var xboxManager = XboxFactory.GetXboxManager("DarkV1p3r"); xboxManager.GetGamer( (xboxGamer) => { // Need to move to the main UI thread. Dispatcher.BeginInvoke(new Action<XboxGamer?>(DisplayGamerDetails),xboxGamer); }); } void DisplayGamerDetails(XboxGamer? xboxGamer) { if (xboxGamer.HasValue) { var profile = xboxGamer.Value.Profile[0]; imgAvatar.Source = new BitmapImage(new Uri(profile.ProfilePictureMiniUrl)); txtUserName.Text = profile.GamerTag; txtGamerScore.Text = int.Parse(profile.GamerScore).ToString("G 0,000"); txtZone.Text = profile.PlayerZone; } else { txtUserName.Text = "Failed to load data"; } } 正如您所看到的,异步编程可能会变得非常混乱. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows – 如何从以前的会话中搜索Powershell命令历史记录
- windows-server-2003 – 将共享文件夹和用户配置文件夹移动
- 未经身份验证的用户如何访问Windows共享?
- Windows 2008 R2 开启磁盘清理的方法
- 在Windows / Active Directory环境中的服务器上配置GIT
- Windows用Eclipse来开发hadoop的WordCount的helloworld
- windows-server-2008 – Windows系统管理员,要知道的事情?
- .net – 如何通过webclient下载图像(jpg)并保存到Windows P
- windows-server-2008 – 在Windows Server上使用内置软件-R
- windows下用idea编写wordcount单词计数项目并打jar包上传到
- Windows API一日一练 56 SetEndOfFile和GetFileS
- windows – 如何修复损坏的ntbackup(.bkf)文件?
- windows-8 – ManipulationMode解释
- 石大iCal课表使用指南
- windows – 使用Dos批处理文件重命名多个文件
- 在Windows上的Visual Studio代码中调试C#代码
- windows-server-2003 – 不存在的服务获得启动控
- windows-8 – 如何模拟Windows RT
- 如何在WiX中设置Windows Package Installer图标而
- 从Windows 10主机将USB Android设备挂载到Docker