0

我正在尝试创建一个后台代理来定期更新 Windows Phone 上用户的动态磁贴。

目前,我的代理代码是:

    string where = "";
    private GeoCoordinate MyCoordinate = null;
    HttpWebResponse webResponse;
    ...
    protected override void OnInvoke(ScheduledTask task)
    {
        System.Diagnostics.Debug.WriteLine("Invoked");
        findMe();

        NotifyComplete();
    }

    private void ResponseCallback(IAsyncResult asyncResult)
    {
        HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
        webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);

        MemoryStream tempStream = new MemoryStream();
        webResponse.GetResponseStream().CopyTo(tempStream);
    }

    private async void findMe()
    {
        Geolocator geolocator = new Geolocator();
        geolocator.DesiredAccuracy = PositionAccuracy.High;

        try
        {
            Geoposition currentPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10));

            MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);

            // var uri = new Uri("http://www.streetdirectory.com//api/?mode=nearby&act=location&output=json&callback=foo&start=0&limit=1&country=sg&profile=template_1&x=" + MyCoordinate.Longitude + "&y=" + MyCoordinate.Latitude + "&dist=1");
            // var client = new HttpClient();

            var webRequest = (HttpWebRequest)HttpWebRequest.CreateHttp("http://www.streetdirectory.com//api/?mode=nearby&act=location&output=json&callback=foo&start=0&limit=1&country=sg&profile=template_1&x=" + MyCoordinate.Longitude + "&y=" + MyCoordinate.Latitude + "&dist=1");
            webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), webRequest);

            System.Diagnostics.Debug.WriteLine("findMe after response");
            System.Diagnostics.Debug.WriteLine(MyCoordinate.Latitude);
            System.Diagnostics.Debug.WriteLine(MyCoordinate.Longitude);
            // var response = await client.GetStringAsync(uri);
            System.Diagnostics.Debug.WriteLine(webResponse.ToString());

            JToken token = JArray.Parse(webResponse.ToString())[0];
            // JToken token = JArray.Parse(response)[0];
            var name = token.Next.First.First;
            var address = token.Next.Last.First;
            where = name + ", " + address;
        }
        catch (Exception)
        {
            System.Diagnostics.Debug.WriteLine("findMe died");
            where = "";
        }
        System.Diagnostics.Debug.WriteLine("findMe complete");
        UpdateAppTile();
    }

    private void UpdateAppTile()
    {
        System.Diagnostics.Debug.WriteLine("UpdateAppTile");
        ShellTile appTile = ShellTile.ActiveTiles.First();
        if (appTile != null)
        {
            StandardTileData tileData = new StandardTileData
            {
                BackContent = where
            };

            appTile.Update(tileData);
        }
        System.Diagnostics.Debug.WriteLine("Update Completed: " + where);
    }

当我尝试运行它时,代码到达webRequest.BeginGetResponse并随后停止。下一行,并ResponseCallback没有到达。

我的代码的旧版本被注释掉了,我认为这是问题,但它也遇到了同样的问题。

4

1 回答 1

3

问题是您NotifyComplete()在回调返回之前调用。

通过调用NotifyComplete您是在告诉操作系统您已完成所有工作并且可以终止代理。显然,当您等待 webrequest 回调时,情况并非如此。

简单的解决方案是将此调用移动到回调方法中。显然,您需要处理错误异常,并且请求超时的时间也比代理等待的时间长。

更改为使用等待代码可能会使您更轻松。

于 2013-07-11T15:13:51.600 回答