从Windows Phone中的LocalStorage获取uri

时间:2013-06-30 22:23:42

标签: windows-phone-8 windows-phone storagefile

我编写以下代码来保存来自互联网的一些图像:

    public static async Task SaveImage(string name, string uri)
    {
        var localfolder = ApplicationData.Current.LocalFolder;
        var client = new HttpClient();

        var imageStream = await client.GetStreamAsync(uri); //Secuencia de bytes
        var storageFile = await localfolder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);

        using (Stream outputStream = await storageFile.OpenStreamForWriteAsync())
        {
            await imageStream.CopyToAsync(outputStream);
        }
    }

我的问题是当我尝试将这些图像存储在本地存储中时存储到CycleTile,因为这个类需要Uri,而我不知道如何在这里提供uri。这就是我所拥有的:

        CycleTileData cycleicon = new CycleTileData();
        cycleicon.Title = "Fotopantalla";
        cycleicon.Count = 0;
        cycleicon.SmallBackgroundImage = new Uri("/Assets/Tiles/FlipCycleTileSmall.png", UriKind.RelativeOrAbsolute);
        List<Uri> images = new List<Uri>();

        for (int i = 0; i < 9; i++)
        {
            /// tries...
            string path1 = "ms-appdata:///local/image" + i + ".jpg";
            string path2 = "isostore:/Shared/ShellContent/image" + i + ".jpg";
            string path3 = "ms-appdata:///Local/Shared/ShellContent/image" + i + ".jpg";
            ///

            Uri uri = new Uri(path2, UriKind.RelativeOrAbsolute);
            images.Add(uri);
        }

        cycleicon.CycleImages = images;

我错了什么或我错过了什么?

1 个答案:

答案 0 :(得分:0)

对于任何与ShellTileData相关的数据结构,您必须使用path2: “isostore:/ Shared / ShellContent / *”如果图像不在(只读)InstalledLocation文件夹中。

有关详细信息,请参阅:http://blogs.msdn.com/b/andy_wigley/archive/2013/04/10/live-apps-creating-custom-tile-and-lock-screen-images.aspx

我的代码中有类似内容:

var store = IsolatedStorageFile.GetUserStoreForApplication();
if (!store.DirectoryExists("Shared/ShellContent"))
{
    store.CreateDirectory("Shared/ShellContent");
}
StorageFolder shellContent = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("Shared", CreationCollisionOption.OpenIfExists);
shellContent = await shellContent.CreateFolderAsync("ShellContent", CreationCollisionOption.OpenIfExists);

Stream imgin = picture.GetImage(); 
//Picture read stream from Media Library or other input stream

StorageFile new_img = await shellContent.CreateFileAsync(newPictureName);
Stream imgout = await new_img.OpenStreamForWriteAsync();

imgin.CopyTo(imgout);

imgout.Close(); // <- necessary in your code or not?
imgin.Close(); // <-

我不确定,一开始是否真的需要Isostore的东西,但如果我在缩短代码时没有做过一些愚蠢的错误,它就可以了。 ; - )

另请参阅Microsoft的开发人员中心的“StandardTileData.BackgroundImage属性”,“Windows Phone的数据”和“如何使用Windows Phone的Isolated Storage Explorer工具”。 (最后一个是关于如何查看设备上保存的图像文件。)