0

我正在尝试让我的 Windows 8“Metro”应用程序工作,这样当我添加磁贴时,我可以使用从互联网动态获取的图像。我知道我无法将 Windows.UI.StartScreen.SecondaryTile uriLogo 设置为远程图像 uri。

目前我有以下代码

var uriImage = "http://www.myURL.com/images/ffe8e8rq.jpg";
var uriLogo = new Windows.Foundation.Uri(uriImage);

...

var tile = new Windows.UI.StartScreen.SecondaryTile(Scenario1TileId,
            "Title text shown on the tile",
            "Name of the tile the user sees when searching for the tile",
            newTileActivationArguments,
            Windows.UI.StartScreen.TileOptions.showNameOnLogo,
            uriLogo);

uriLogo 引发类型异常,我知道我需要将图像从 uriImage 存储到本地文件然后引用它,但谁能给我建议从哪里开始或者我可以参考的示例。在查看文档后,我仍然被困在哪里开始这个。

4

1 回答 1

1

是的,我有一些代码可以做到这一点,来自这个链接。它在 C# 中,因此您必须翻译成 JavaScript。

/// <summary>
/// Copies an image from the internet (http protocol) locally to the AppData LocalFolder.  This is used by some methods 
/// (like the SecondaryTile constructor) that do not support referencing images over http but can reference them using 
/// the ms-appdata protocol.  
/// </summary>
/// <param name="internetUri">The path (URI) to the image on the internet</param>
/// <param name="uniqueName">A unique name for the local file</param>
/// <returns>Path to the image that has been copied locally</returns>
private async Task<Uri> GetLocalImageAsync(string internetUri, string uniqueName)
{
     if (string.IsNullOrEmpty(internetUri))
     {
         return null;
     }

     using (var response = await HttpWebRequest.CreateHttp(internetUri).GetResponseAsync())
     {
         using (var stream = response.GetResponseStream())
         {
             var desiredName = string.Format("{0}.jpg", uniqueName);
             var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceExisting);

             using (var filestream = await file.OpenStreamForWriteAsync())
             {
                 await stream.CopyToAsync(filestream);
                 return new Uri(string.Format("ms-appdata:///local/{0}.jpg", uniqueName), UriKind.Absolute);
             }
         }
     }
}
于 2013-05-08T03:52:50.340 回答