使用远程图像创建 CycleTile [英] Creating CycleTile with remote images

查看:19
本文介绍了使用远程图像创建 CycleTile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过许多展示 CycleTile 实际效果的示例,但这些示例都使用了本地图像.一旦应用程序首次运行并将 CycleTile 指向远程图像,是否可以设置这些图像?或者,如果我确实需要先将这些保存到手机中,我怎样才能让 CycleTile 引用它们?

I have seen a number of examples showing the CycleTile in action, but these have all used local images. Is it possible to set these images once the app is first run and point the CycleTile to the remote images? Or if I do need to save these to the phone first, how can I get the CycleTile to reference them?

推荐答案

CycleTileTemplate &CycleTileData 仅支持本地 URI,不支持远程 Web URI.这意味着您只能从 XAP 安装的文件或 IsoStore 中的文件设置循环映像的源.

CycleTileTemplate & CycleTileData only support local URIs and don't support remote web URIs. Meaning that you can only set the source of a cycle image from files installed from the XAP or from files in IsoStore.

为了支持 CycleTileData 中的远程图像,您需要在周期性后台代理中下载图像,将它们保存到 IsoStore,然后使用这些图像更新 CycleTileData.推送通知在这里不起作用,因为图像需要是本地的,ShellTileSchedule 也不会.

In order to support remote images in CycleTileData, you'll need to download the images in a periodic background agent, save them to IsoStore and then update the CycleTileData with those images. Push notifications won't work here since the images need to be local and neither will ShellTileSchedule.

确保将图像保存到/Shared/ShellContent"下的 IsoStore 并将其 URI 设置为isostore:/Shared/Shellcontent/myImage.png",否则开始屏幕图块将无法访问它们.

Make sure to save the images to IsoStore under the "/Shared/ShellContent" and set their URIs as "isostore:/Shared/Shellcontent/myImage.png" or they won't be accessible to the start screen tiles.

让我们看一个例子.首先,我们开始编写一个并行线程算法,该算法启动 9 个下载线程,等待结果,然后更新图块:

Let's see an example of that. First we start off by writing up a parallelized threaded algorithm that launches 9 download threads, waits for the results and then updates tiles:

private IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
        var threadFinishEvents = new List<WaitHandle>();

        DownloadImages(threadFinishEvents);

        new Thread(()=>
        {
            Mutex.WaitAll(threadFinishEvents.ToArray());

            UpdateTiles();

            isoStore.Dispose();
        }).Start();
}

接下来,我们将 9 个图像下载到 IsoStore/Shared/ShellContent".我们将特别注意为每个 Web 下载添加新的线程标记,并在文件位于 IsoStore 后将标记设置为已完成.

Next, we'll download the 9 images into IsoStore "/Shared/ShellContent". We'll take special note to add the new threading flags for each web download, and set the flag as done once the file is in IsoStore.

private void DownloadImages(List<WaitHandle> threadFinishEvents)
{
    for (int i = 0; i < 9; i++)
    {
        var localI = i;

        var threadFinish = new EventWaitHandle(false, EventResetMode.ManualReset);
        threadFinishEvents.Add(threadFinish);

        var request = WebRequest.CreateHttp("http://www.justinangel.net/storage/691x336.png");
        request.BeginGetResponse(ir =>
        {
            var result = request.EndGetResponse(ir);
            using (var isoStoreFile = isoStore.OpenFile("shared/shellcontent/myImage" + localI + ".png",
                                                        FileMode.Create,
                                                        FileAccess.ReadWrite))
            using (var response = result.GetResponseStream())
            {
                var dataBuffer = new byte[1024];
                while (response.Read(dataBuffer, 0, dataBuffer.Length) > 0)
                {
                    isoStoreFile.Write(dataBuffer, 0, dataBuffer.Length);
                }
            }

            threadFinish.Set();
        }, null);
    }
}

最后,我们将更新实时磁贴以使用 IsoStore 中的新图像.

Finally, we'll update the live tile to use the new images in IsoStore.

private void UpdateTiles()
{
    ShellTile.ActiveTiles
        .First()
        .Update(new CycleTileData()
        {
            Title = "Cyclical",
            CycleImages = new Uri[]
            {
                new Uri("isostore:/Shared/ShellContent/myImage0.png", UriKind.Absolute), 
                new Uri("isostore:/Shared/ShellContent/myImage1.png", UriKind.Absolute), 
                new Uri("isostore:/Shared/ShellContent/myImage2.png", UriKind.Absolute), 
                new Uri("isostore:/Shared/ShellContent/myImage3.png", UriKind.Absolute), 
                new Uri("isostore:/Shared/ShellContent/myImage4.png", UriKind.Absolute), 
                new Uri("isostore:/Shared/ShellContent/myImage5.png", UriKind.Absolute), 
                new Uri("isostore:/Shared/ShellContent/myImage6.png", UriKind.Absolute), 
                new Uri("isostore:/Shared/ShellContent/myImage7.png", UriKind.Absolute), 
                new Uri("isostore:/Shared/ShellContent/myImage8.png", UriKind.Absolute), 
            }
        });
}

有几个有趣的事情需要考虑:

There's a couple of interesting things to consider:

  1. 周期性后台代理只有 25 秒的时间来完成其操作,因此在激活 Mutex.WaitAll 时添加计时器阈值并使其正常失败可能是有意义的.
  2. 在某些网络条件下,在 25 秒内下载 9 个图像可能根本不起作用,因此最好为此进行优化.您可以使用较少的图像,也可以每 30 分钟仅更新几张图像.
  3. 将 CycleTileData 更新为相同的文件 URI 不会触发磁贴更新 (AFAIK).因此,您需要比 myImage0 更好的文件名,但图像的文件名应该是唯一的.

这篇关于使用远程图像创建 CycleTile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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