2013-06-30 37 views
0

我寫了下面的代碼從互聯網上節省一些圖片:獲取一個URI

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; 

我是什麼錯誤或我錯過了什麼?

+0

其實,應該工作(路徑2或3)。如果下載工作正常,您是否使用WP Power Tools或其他Isostorage工具進行檢查? – blakharaz

回答

0

對於任何與ShellTileData相關的數據結構,如果圖像不在(只讀)InstalledLocation文件夾中,則必須使用path2: 「isostore:/ Shared/ShellContent/*」。

有關詳細信息,請參閱: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 Property」,「Windows Phone數據」和「如何使用Windows Phone的獨立存儲瀏覽器工具」。 (最後一個是關於如何看看你的設備上保存的圖像文件。)