Windows Phone 8.1实时磁贴每天不更新

时间:2014-09-26 21:31:55

标签: windows-phone-8.1 live-tile

我每天(早上7点左右)自行更新我的Windows Phone 8.1动态磁贴更新

我的项目有两部分:

  • 在Azure上托管的Web API项目,以XML格式提供切片模板(该部分工作正常)
  • 一个Windows Phone 8.1项目,其中包含一个MainPage.xaml,我在其中构建了2个方形/宽的瓷砖

当我将应用程序从应用程序列表固定到开始屏幕时,我的磁贴(无论我选择正方形还是宽屏)都可以在当天的两个品牌之间来回切换。 第二天早上7点15分,瓷砖不会更新2个新品牌:(

我注意到,如果我点击磁贴启动应用并返回到开始屏幕,则磁贴已更新。 如果我取消拼接并重新拼贴瓷砖,它也可以工作,但在这两种情况下,这都是欺骗" : - /

我将这样的Azure网站称为以获取两个品牌的磁贴模板: http://example.azurewebsites.net/api/tiles/0(BRAND1)

http://example.azurewebsites.net/api/tiles/1(BRAND2)

XML切片模板如下所示(示例):

<tile>
    <visual>
        <binding template="TileWide310x150ImageAndText01">
            <image id="1" src="http://www.example.com/sales/BRAND1/visualWide.png"/>
            <text id="1">Brand 1</text>
        </binding>
        <binding template="TileSquare150x150PeekImageAndText04">
          <image id="1" src="http://www.example.com/sales/BRAND1/visualSquare.png"/>
          <text id="1">Brand 1</text>
        </binding>
    </visual>
 </tile>

WP8.1页面包含:

public MainPage()
{
    this.InitializeComponent();

    this.NavigationCacheMode = NavigationCacheMode.Required;

    TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);
    PeriodicUpdateRecurrence recurrence = PeriodicUpdateRecurrence.Daily;

    Task<string> responseBody;

    for (int i = 0; i < 2; i++)
    {
    using (var httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri("http://xxxxx.azurewebsites.net/api/tiles/" + i); //I hid the address here for discretion
        HttpResponseMessage response = httpClient.GetAsync(httpClient.BaseAddress).Result;
        string statusCode = response.StatusCode.ToString();
        response.EnsureSuccessStatusCode();
        responseBody = response.Content.ReadAsStringAsync();
    }

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(responseBody.Result);

    string srcWide, srcSquare, textWide, textSquare, templateWide, templateSquare;

    XElement root = XElement.Parse(responseBody.Result);

    IEnumerable<XElement> wideElement = root.Descendants("binding").Where(a => a.Attribute("template").Value.ToLower().Contains("wide"));
    IEnumerable<XElement> squareElement = root.Descendants("binding").Where(a => a.Attribute("template").Value.ToLower().Contains("square"));

    templateWide = wideElement.Attributes("template").SingleOrDefault().Value;
    srcWide = wideElement.Descendants("image").Attributes("src").SingleOrDefault().Value;
    textWide = wideElement.Descendants("text").SingleOrDefault().Value;

    templateSquare = squareElement.Attributes("template").SingleOrDefault().Value;
    srcSquare = wideElement.Descendants("image").Attributes("src").SingleOrDefault().Value;
    textSquare = wideElement.Descendants("text").SingleOrDefault().Value;

    XmlDocument wideTileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150ImageAndText01);
    XmlNodeList wideTileTextAttributes = wideTileXml.GetElementsByTagName("text");
    wideTileTextAttributes[0].InnerText = textWide;
    XmlNodeList wideTileImageAttributes = wideTileXml.GetElementsByTagName("image");
    ((XmlElement)wideTileImageAttributes[0]).SetAttribute("src", srcWide);
    ((XmlElement)wideTileImageAttributes[0]).SetAttribute("alt", textWide);

    XmlDocument squareTileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150PeekImageAndText01);
    XmlNodeList squareTileTextAttributes = squareTileXml.GetElementsByTagName("text");
    squareTileTextAttributes[0].AppendChild(squareTileXml.CreateTextNode(textSquare));
    XmlNodeList squareTileImageAttributes = squareTileXml.GetElementsByTagName("image");
    ((XmlElement)squareTileImageAttributes[0]).SetAttribute("src", srcSquare);
    ((XmlElement)squareTileImageAttributes[0]).SetAttribute("alt", textSquare);


    IXmlNode node = wideTileXml.ImportNode(squareTileXml.GetElementsByTagName("binding").Item(0), true);
    wideTileXml.GetElementsByTagName("visual").Item(0).AppendChild(node);

    TileNotification tileNotification = new TileNotification(wideTileXml);
    tileNotification.ExpirationTime = DateTimeOffset.UtcNow.AddDays(1).AddHours(7).AddMinutes(0);
    TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);
    }

    List<Uri> urisToPoll = new List<Uri>(5);
    urisToPoll.Add(new Uri("http://example.azurewebsites.net/api/tiles/0")); //fake url
    urisToPoll.Add(new Uri("http://example.azurewebsites.net/api/tiles/1")); //fake url

    //I want the tile to update its content every day at 7:15am
    //(the Azure website supposedly returns new brands of the day at 7am)
    DateTime dtTomorrow7 = DateTime.SpecifyKind(DateTime.Today.AddDays(1).AddHours(7).AddMinutes(15), DateTimeKind.Local);
    DateTimeOffset dtoTomorrow7 = dtTomorrow7;
    TileUpdateManager.CreateTileUpdaterForApplication().StartPeriodicUpdateBatch(urisToPoll, dtoTomorrow7, recurrence);
}

我不确定我是否正确使用 StartPeriodicUpdateBatch 方法。 在调试模式下,我可以看到DateTimeOffset值为 Day + 1 7:15 am +2:00 ,这看起来是正确的,但是磁贴不会更新。 我住在法国的UTC + 2 谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

您的dtTomorrow7不正确。它应该是上午7:15。然后它将首先在上午7:15进行更新,之后以定期间隔进行后续更新(您将其设置为每日)。

最后一个TimeSpan偏离UTC的开始时间示例:

    DateTime tomorrow = DateTime.Today.AddDays(1);
    var startTime = new DateTimeOffset(tomorrow.Year, tomorrow.Month, tomorrow.Day, 7, 15, 0, TimeSpan.FromHours(2));