2012-06-03 81 views
0

在我的windows phone 7應用程序中,我創建了一個按鈕,可以在不改變背景的情況下更改背景。數量。默認情況下是有背景圖片。我需要的是,如果用戶更改了背景(使用上述按鈕),我希望該圖像應該被隔離存儲器保存或記住。然後,當應用程序再次啓動時,該特定圖像(上次由用戶選擇)應該是我的應用程序的背景圖像。現在的問題是,我無法檢索圖像(但能夠保存圖像),並且不知道如何將該圖像作爲背景圖像在應用程序啓動時自動放置(不想使用任何按鈕來保存或檢索,必須是自動的)。有誰能夠幫助我?非常感謝您的辛勤工作!在應用程序啓動時自動保存並檢索背景圖像

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     string imguri = ""; 

     click_count = click_count % 4; 
     switch (click_count) 
     { 
      case 0: imguri = "Images/image4.png"; break; 
      case 1: imguri = "Images/image3.jpg"; break; 
      case 2: imguri = "Images/image2.png"; break; 
      case 3: imguri = "Images/image1.jpg"; break; 

     } 
     click_count++; 

     BitmapImage bitmapImage = new BitmapImage(new Uri(imguri, UriKind.Relative)); 
     ImageBrush imageBrush = new ImageBrush(); 
     imageBrush.ImageSource = bitmapImage; 
     var app = Application.Current as App; 
     this.LayoutRoot.Background = imageBrush; 
     app.appbrush = imageBrush; 
     app.backchanged = true; 

     string filename = "Images/app.jpg"; 
     StreamResourceInfo sr = Application.GetResourceStream(new Uri(filename, UriKind.Relative)); 

     bitmapImage.SetSource(sr.Stream); 
     WriteableBitmap wb = new WriteableBitmap(bitmapImage); 

     using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      myIsolatedStorage.CreateDirectory("Images"); 

      if (myIsolatedStorage.FileExists(filename)) 
      { 
       myIsolatedStorage.DeleteFile(filename); 
      } 
      IsolatedStorageFileStream filestream = myIsolatedStorage.CreateFile(filename); 
      Extensions.SaveJpeg(wb, filestream, wb.PixelWidth, wb.PixelHeight, 0, 100); 
      filestream.Close(); 
     } 
    } 

回答

0

嘗試加載圖像的代碼,並將其分配給背景:

IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication(); 

if (Store.FileExists("Images/app.jpg")) 
{ 
    try 
    { 
     using (IsolatedStorageFileStream Stream = new IsolatedStorageFileStream(Path, FileMode.Open, FileAccess.Read, Store)) 
     { 
      if (Stream.Length > 0) 
      { 
       BitmapImage Image = new BitmapImage(); 
       Image.SetSource(Stream); 
       this.LayoutRoot.Background = Image; 
      } 
     } 
    } 
    catch (Exception) 
    {    
    } 
} 
+0

如果把你的代碼時,應用程序啓動是自動加載圖像? – Mac

+0

給頁面構造函數 – Ku6opr

相關問題