如何将图像路径保存到Live Tile的WP8 Local文件夹中

时间:2014-01-27 17:38:31

标签: c# windows-phone-8 windows-phone

我正在更新我的Windows Phone应用程序以使用新的WP8文件存储API( LocalFolder )而不是WP7 API( IsolatedStorageFile )。

旧工作方法

以下是使用 IsolatedStorageFile 方法将图像成功保存到 / Shared / ShellContent 文件夹的方法(保存图像时请注意/Shared/ShellContent/前缀,以及更新实时图块时的isostore:/Shared/ShellContent/前缀。

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
     if (myIsolatedStorage.FileExists("/Shared/ShellContent/LiveTileImagePath.jpg"))
     {
          myIsolatedStorage.DeleteFile("/Shared/ShellContent/LiveTileImagePath.jpg");
     }

     IsolatedStorageFileStream copyFileStream = myIsolatedStorage.CreateFile("/Shared/ShellContent/LiveTileImagePath.jpg");
     wb.SaveJpeg(copyFileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
     copyFileStream.Close();

     //UpdateTile
     ShellTile tile = ShellTile.ActiveTiles.First();

     if (tile != null)
     {
            FlipTileData tileData = new FlipTileData()
            {
                Title = "title",
                BackTitle = "backtitle",
                BackContent = "",
                WideBackContent = "",
                SmallBackgroundImage = new Uri("/Images/SmallIconBW.png", UriKind.RelativeOrAbsolute),
                BackgroundImage = new Uri("/Images/MediumKiss336.png", UriKind.RelativeOrAbsolute),
                BackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute),
                WideBackgroundImage = new Uri("/Images/MediumKiss336.png", UriKind.RelativeOrAbsolute),
                WideBackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute)
                };

                tile.Update(tileData);
            }
}

要解决的问题

当我使用新方法保存图像时出现问题。我似乎无法找到更新瓷砖的正确图像路径。这是我保存图像的新方法(注意fileStream是一个MemoryStream,它包含我从方法前面的WriteableBitmap生成的jpeg图像。它可以节省存储空间以供使用应用程序,但不是共享文件夹)

StorageFile liveTileImageFile = await localFolder.CreateFileAsync("/Shared/ShellContent/LiveTileImagePath.jpg", CreationCollisionOption.ReplaceExisting);

fileStream.Seek(0, SeekOrigin.Begin);

using(Stream sharedFolderStream = await liveTileImageFile.OpenStreamForWriteAsync())
{
    await fileStream.CopyToAsync(sharedFolderStream);
}

ShellTile tile = ShellTile.ActiveTiles.First();

if (tile != null)
{
    FlipTileData tileData = new FlipTileData()
    {
        Title = "Kiss Dictionary",
        BackTitle = "name",
        BackContent = "",
        WideBackContent = "",
        SmallBackgroundImage = new Uri("/Images/SmallIconBW.png", UriKind.RelativeOrAbsolute),
        BackgroundImage = new Uri("/Images/MediumKiss336.png", UriKind.RelativeOrAbsolute),
        BackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute),
            WideBackgroundImage = new Uri("/Images/MediumKiss336.png", UriKind.RelativeOrAbsolute),
            WideBackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute)
         };

         tile.Update(tileData);
}

问题是我无法将图像保存到Shared / ShellFolder,我收到此异常消息:

文件名,目录名或卷标语法不正确。 (HRESULT异常:0x8007007B)

如果您更新了应用的LiveTile更新,请说明您用于保存的路径以及分配给LiveTile图像的文件路径。

更新

Rob的回答如下修复了异常并允许我保存到该文件夹​​。我对我的磁贴更新中使用的文件路径进行了更改(感谢Scott Lovegrove的建议):

而不是旧的方式:

BackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute)

我成功地使用了这个

BackBackgroundImage = new Uri("isostore:shared\\shellcontent\\LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute)

1 个答案:

答案 0 :(得分:4)

这很难看,但它确实有效......您需要深入查看隔离存储的每个目录。你不能只是放入完整的路径(这太容易了)。

所以而不是:

StorageFile liveTileImageFile = await localFolder.CreateFileAsync("/Shared/ShellContent/LiveTileImagePath.jpg", CreationCollisionOption.ReplaceExisting);

你应该:

var sharedFolder = await localFolder.GetFolderAsync("Shared");
var shellContentFolder = await sharedFolder.GetFolderAsync("ShellContent");
var liveTileImageFile = await shellContentFolder.CreateFileAsync("LiveTileImagePath.jpg", CreationCollisionOption.ReplaceExisting);

希望有所帮助。