实时磁贴未更新 [英] Live Tiles not Updating

查看:110
本文介绍了实时磁贴未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个后台代理,以定期更新Windows Phone上用户的Live Tiles.

I'm trying to create a background agent that periodically updates a user's Live Tiles on Windows Phone.

当前,我的代理代码是:

Currently, my code for the agent is:

    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.

When I attempt to run this, the code reaches webRequest.BeginGetResponse and subsequently stops. The next line, and ResponseCallback are not reached.

我的代码的旧版本已被注释掉,我以为是问题所在,但也遇到了同样的问题.

An older version of my code is commented out, which I thought was the problem but it experienced the same problem as well.

推荐答案

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

The problem is that you are calling NotifyComplete() before the callback returns.

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

By calling NotifyComplete you're telling the OS that you've finished all your work and the agent can be terminated. Obviously this isn't the case when you're waiting for your webrequest callback.

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

The simple solution is to move this call into the callback method. obviously you'll need to handle error exceptions and the request timeout taking longer than the agent will wait for as well though.

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

Changing to using awaitable code may make this easier for you.

这篇关于实时磁贴未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆