Windows8异步编程的注意事项

简介:

Windows8 App开发中涉及到下载数据和上传数据。针对小的数据使用的是WinJS.xhr(),而针对大的文件,不方便使用post方式直接上传的文件都使用BackgroundTransfer进行传输。因为Windows8 App虽然后台运行程序也是一个类似IE的进程,但是app和网页在开发中还是有一些不同,所以以下有两点总结。

 

1.Ajax请求,相同的url会缓存。

WWAHost.exe也会像IE一样,对相同的url返回的数据进行缓存。一般app都会进行CRUD操作,所以当我们进行操作完之后,我们必须更新数据。但是在一开始我更新了数据,但是界面始终不更新。

var rand = Math.random();
//为了演示,这里直接用了一个随机数,实际开发中最好以当前时间为种子的随机数,防止缓存
    WinJS.xhr({ url: "http://localhost:50623/Default.aspx?t="+rand })
        .done(function complete(result) {
          //do something.
        }, function error(result) {
            console.log(result.responseText);
        });
 
这样我们每次请求default.aspx返回的数据应该是最新的。ASP.NET推出Web API之后,后台最好不要写.aspx页面或者hanler,最好都使用Web API,这样传统的web应用,mobile app和Windows8 app都可以调用相同的数据服务接口。相对而已比后台使用WCF还是要方便一些。
 
2.Ajax的异步。
在Windows8 App开发中,这一点开发人员始终要牢记,否则代码出了问题,根本无法定位到代码哪里出错了。原先代码如下:
var userInfo = null;
    function ShowUser() {
        var userid = 10001;
        GetUserInfoByAjax(userid);
        showUserInfo();
    }
    function GetUserInfoByAjax(userid) {
        var random = Math.random();
        WinJS.xhr({url:"http://localhost:50623/userinfo.aspx?userid="+userid+"&t="+rand})
            .done(function complete(result) {
            //set the userInfo.
            userInfo = JSON.parse(result.responseText);
        }, function error(result) {
            Console.log(result.responseText);
        });
    }
    function showUserInfo(){
        document.getElementById("username").innerHTML=userInfo.username;
        document.getElementById("hometown").innerHTML=userInfo.hometown;
    }
 
自己一股劲的在调试,但是界面一直不显示username和hometown,断点显示userInfo为null。但是在GetUserInfoByAjax()方法体里面userInfo确实取得了服务器返回的数据。因为前端和后台代码都是自己写,所以有时真不确定是哪头的代码出了问题,所以有时单元测试特别的重要。这里又要提到Web API,等项目结束之后把后台的代码重构为web api出来。
 
其实这个问题新手很容易范,因为GetUserInfoByAjax(userid);在showUserInfo(); 之前,所以肯定是先获得了userInfo,但是为什么在showUserInfo()里面的userInfo为null。我在写代码时,把Ajax异步请求的概念忘了,WinJS.xhr()发送请求之后不能马上获得数据,这就是异步。所以在代码执行到showUserInfo()内部时,可能userInfo还没有重新赋值,所以还是null。旁边的同事帮我看了一眼就发现我的错误做法。
 
在获得Ajax数据之后要执行的事情全部要放到Ajax成功方法体内,代码如下:
function GetUserInfoByAjax(userid) {
        var random = Math.random();
        WinJS.xhr({url:"http://localhost:50623/userinfo.aspx?userid="+userid+"&t="+rand})
            .done(function complete(result) {
            //set the userInfo.
            userInfo = JSON.parse(result.responseText);
            showUserInfo();
        }, function error(result) {
            Console.log(result.responseText);
        });
    }
 
程序运行正常。从以上来看,我们在web开发过程中,很多问题都是由于我们对基础概念掌握的不好,或者忽略了最基本的概念,等到程序代码一多,问题就显得非常多,并且自己经常处在“不识庐山真面目,只缘身在此山中”。一直在自己写的错误代码里面打转。
目录
相关文章
|
vr&ar Windows 编译器
重新想象 Windows 8 Store Apps (44) - 多线程之异步编程: 经典和最新的异步编程模型, IAsyncInfo 与 Task 相互转换
原文:重新想象 Windows 8 Store Apps (44) - 多线程之异步编程: 经典和最新的异步编程模型, IAsyncInfo 与 Task 相互转换 [源码下载] 重新想象 Windows 8 Store Apps (44) - 多线程之异步编程: 经典和最新的异步编程模型, IA...
976 0
|
Windows
重新想象 Windows 8 Store Apps (45) - 多线程之异步编程: IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress
原文:重新想象 Windows 8 Store Apps (45) - 多线程之异步编程: IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress [源码下载] 重新想象 Window...
803 0
|
6天前
|
安全 Unix 虚拟化
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 2 月更新)
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 2 月更新)
32 11
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 2 月更新)
|
6天前
|
Unix 虚拟化 Windows
Windows Server 2025 中文版、英文版下载 (2025 年 2 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 2 月更新)
28 7
Windows Server 2025 中文版、英文版下载 (2025 年 2 月更新)
|
6天前
|
安全 虚拟化 Windows
Windows Server 2019 中文版、英文版下载 (2025 年 2 月更新)
Windows Server 2019 中文版、英文版下载 (2025 年 2 月更新)
41 22
|
13天前
|
安全 网络安全 数据安全/隐私保护
Windows Server 2025 Active Directory 重置用户密码
密码重置是管理员日常任务之一,用户因忘记或多次输错密码导致账户锁定时需进行重置。本文介绍在Active Directory服务器上重置密码的三种方法。
|
2月前
|
安全 关系型数据库 MySQL
Windows Server 安装 MySQL 8.0 详细指南
安装 MySQL 需要谨慎,特别注意安全配置和权限管理。根据实际业务需求调整配置,确保数据库的性能和安全。
302 9