2012-12-12 5 views
0

는 내가 다른 페이지로 라이브러리에서 선택한 이미지를 전달할 수있는 방법, NavigationService.Navigate(new Uri("/Second.xaml?msg=mesage", UriKind.Relative));Windows Phone에서 페이지간에 사진을 전달하는 방법은 무엇입니까?

질문은 통해 데이터를 전달하는 방법을 알아?

이미지를 선택하려면, 내가 사용하는 PhotoChooserTask과이 완료된 경우에 나는이 있습니다

private void photoChooserTask_Completed(object sender, PhotoResult e) 
    { 
     if (e.ChosenPhoto != null) 
     { 
      BitmapImage image = new BitmapImage(); 
      image.SetSource(e.ChosenPhoto); 
      this.img.Source = image; 
     } 
    } 

어떻게 다른 페이지로 선택한 사진을 보낼 수 있습니까? 버퍼에 쓰거나, 전역 변수를 설정하거나, 격리 된 저장소에 '저장'해야합니까?

답변

1
  1. 먼저 IsolatedStorage에서 그림을 저장하고 파일 경로를 문자열 매개 변수로 다른 페이지로 전달하고 필요할 때 그림을로드 할 수 있습니다.

  2. PhoneApplicationService를 사용하여 이미지를 State로 저장하고 필요할 때로드하십시오. IsolatedStorage에서 사진을 읽는

    public static void SaveStreamToStorage(Stream imgStream, string fileName) 
         { 
          using (IsolatedStorageFile iso_storage = IsolatedStorageFile.GetUserStoreForApplication()) 
          { 
           //Structure the path you want for your file. 
           string filePath = GetImageStorePathByFileName(fileName); 
    
           //Constants.S_STORE_PATH is the path I want to store my picture. 
           if (!iso_storage.DirectoryExists(Constants.S_STORE_PATH)) 
           { 
            iso_storage.CreateDirectory(Constants.S_STORE_PATH); 
           } 
           //I skip the process when I find the same file. 
           if (iso_storage.FileExists(filePath)) 
           { 
            return; 
           } 
    
           try 
           { 
            if (imgStream.Length > 0) 
            { 
             using (IsolatedStorageFileStream isostream = iso_storage.CreateFile(filePath)) 
             { 
              BitmapImage bitmap = new BitmapImage(); 
              bitmap.SetSource(imgStream); 
    
              WriteableBitmap wb = new WriteableBitmap(bitmap); 
    
              // Encode WriteableBitmap object to a JPEG stream. 
              Extensions.SaveJpeg(wb, isostream, wb.PixelWidth, wb.PixelHeight, 0, 100); 
    
              isostream.Close(); 
    
              bitmap.UriSource = null; 
              bitmap = null; 
              wb = null; 
             } 
            } 
           } 
           catch(Exception e) 
           { 
            if (iso_storage.FileExists(filePath)) 
             iso_storage.DeleteFile(filePath); 
    
            throw e; 
           } 
          } 
         } 
    

    샘플 : IsolatedStorage에 저장하는

샘플

public static BitmapImage LoadImageFromIsolatedStorage(string imgName) 
     { 
      try 
      { 
       var bitmapImage = new BitmapImage(); 
       //bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation; 
       using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        //Check if file exists to prevent exception when trying to access the file. 
        if (!iso.FileExists(GetImageStorePathByFileName(imgName))) 
        { 
         return null; 
        } 
        //Since I store my picture under a folder structure, I wrote a method GetImageStorePathByFileName(string fileName) to get the correct path. 
        using (var stream = iso.OpenFile(GetImageStorePathByFileName(imgName), FileMode.Open, FileAccess.Read)) 
        { 
         bitmapImage.SetSource(stream); 
        } 
       } 
       //Return the picture as a bitmapImage 
       return bitmapImage; 
      } 
      catch (Exception e) 
      { 
       // handle the exception 
       Debug.WriteLine(e.Message); 
      } 

      return null; 
     } 
0

당신은 너무 (나는 언어 지원을 위해 사용하는 단지 코드 샘플 변수 이름을 상관하지 않습니다) 당신의 App.xaml.cs를에 정의 된 변수를 사용하여 같은 다른 페이지를 호출 할 수 있습니다

private LanguageSingleton LanguageInstance 
{ 
    get 
    { 
     return (App.Current as App).Language; 
    } 
} 
을 여기

는 그 변수 정의 할 수있는 방법입니다 :

public LanguageSingleton Language { get; set; } 

내가이 일을 더 가지 방법이 있습니다 확신을하지만,이 하나 개의 솔루션입니다.

관련 문제