WP8快速应用程序简历使用异步方法



在我的Windows Phone 8应用中,我正在使用异步方法从服务器检索数据。

实施Fast App Resume功能后,我对我来说还有另一个问题。从服务器检索数据的异步方法在恢复类型System.Net.WebException时抛出例外。

重现问题的步骤是,当应用程序通过异步方法加载数据时,您只需点击开始按钮即可。

例如,我有一个加载用户通知的页面。我调用了async void GetNotifications()方法,该方法进一步调用以下方法检索响应字符串。

    public async Task<string> Get(string URL)
    {
        var request = WebRequest.Create(new Uri(URL)) as HttpWebRequest;
        if (APIHelper.APIHandshake != null)
            request.Headers["Cookie"] = "ii=" + APIHelper.APIHandshake.Sid + ";";
        return await httpRequest(request);
    } 

下面给出了httprequest方法的实现。

    private async Task<string> httpRequest(HttpWebRequest request)
    {
        string received;
        using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory
            .FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
        {
            using (var responseStream = response.GetResponseStream())
            {
                using (var sr = new StreamReader(responseStream))
                {
                    //cookieJar = request.CookieContainer;
                    //responseCookies = response.Cookies;
                    received = await sr.ReadToEndAsync();
                }
            }
        }
        return received.Replace("[{}]", "[]")
                .Replace("[{},{}]", "[]")
                .Replace(""subcat_id":""", ""subcat_id":"0"");
    }

用户只需单击打开通知页面的菜单,然后立即按电话的开始按钮即可停用应用程序。当用户从开始菜单中单击应用程序瓷砖时,将会抛出异常。

有解决方案吗?停用闲置模式检测会起作用吗?

它可能不是最好的解决方案,但是可以通过在app.xaml.cs页面上的intitalizephoneApplication()方法中添加此代码来取消异步请求。

PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;

在此处阅读有关此属性的更多信息
测试它,它应该解决这个问题,但我并不是假装这是最好的事情....

最新更新