2012-09-15 2 views
0

내 Windows Store (일명 Windows 8) 응용 프로그램은 기본 그리드 응용 프로그램 템플릿을 사용하여 항목을 표시합니다. 항목 템플릿에는 텍스트 정보가 중첩 된 이미지가 포함되어 있습니다. 응용 프로그램의 크기를 줄이기 위해 모든 항목의 이미지를 저장하지 않고 대신 Uri를 이미지가있는 웹 서버에 절대 경로 (http)로 저장합니다. Uri 이미지를 바인딩하기 위해 표준 템플릿을 수정했는데 (Uri를 문자열이 제대로 작동하도록 변환해야만했습니다.) 이제 애플리케이션을 시작할 때마다 이미지가 모두 다운로드되고 이미지 컨트롤에 의해 자동으로 표시됩니다.Windows 8에서 이미지 컨트롤로 이미지를 다운로드 한 후 이미지를 로컬에 저장하는 방법은 무엇입니까?

내가 지금 원하는 것은 다운로드 한 이미지를 자동으로 저장하고 다운로드 한 이미지의 Uris를 로컬 스토리지를 가리키는 이미지로 수정하는 것입니다. 저는 여기에 두 가지 문제에 부딪 : 내 GroupedItemsPage.xaml 바인딩

  • 내가 이쪽 StandardStyles.xaml

에서 전체 ItemTemplate을 바인딩 경우에 발생하는 ImageOpened 이벤트를 얻을 수있다 :

<GridView 
     x:Name="itemGridView" 
     ItemTemplate="{StaticResource Standard250x250ItemTemplate}"> 

<DataTemplate x:Key="Standard250x250ItemTemplate"> 
      <Image Source="{Binding ImageUri}" ImageOpened="Image_ImageOpened"/> 
</DataTemplate> 
:

바운드 템플릿은 이벤트 (StandardStyles.xaml)를 해고 수정

private void Image_ImageOpened(object sender, RoutedEventArgs e) 
    { 

    } 
  • 나는의 컨텐츠를 저장하는 방법을 모른다 :

    Image_ImageOpened 이벤트 핸들러 (`GroupedItemsPage.xaml.cs '), 그러나 결코 화재 코드 숨김 파일에 정의되어 있습니다 이진 파일로 이미지 프레임 워크 요소.

+0

이미지가 표시 되나요? – mydogisbox

+0

@mydogisbox 예, 인터넷 연결이 있으면 모든 이미지가 멋지게 표시됩니다. –

답변

6

또한 로컬로 일부 http 이미지를 복사해야했습니다. 여기에 제 작업 코드가 있습니다. 이 메서드는 internetURI = "http : // wherever-your-image-file-is"및 이미지의 고유 한 이름으로 호출해야합니다. AppData의 LocalFolder 스토리지에 이미지를 복사 한 다음 바인딩에 사용할 수있는 새 로컬 이미지의 경로를 반환합니다. 희망이 도움이!

/// <summary> 
    /// Copies an image from the internet (http protocol) locally to the AppData LocalFolder. This is used by some methods 
    /// (like the SecondaryTile constructor) that do not support referencing images over http but can reference them using 
    /// the ms-appdata protocol. 
    /// </summary> 
    /// <param name="internetUri">The path (URI) to the image on the internet</param> 
    /// <param name="uniqueName">A unique name for the local file</param> 
    /// <returns>Path to the image that has been copied locally</returns> 
    private async Task<Uri> GetLocalImageAsync(string internetUri, string uniqueName) 
    { 
     if (string.IsNullOrEmpty(internetUri)) 
     { 
      return null; 
     } 

     using (var response = await HttpWebRequest.CreateHttp(internetUri).GetResponseAsync()) 
     { 
      using (var stream = response.GetResponseStream()) 
      { 
       var desiredName = string.Format("{0}.jpg", uniqueName); 
       var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceExisting); 

       using (var filestream = await file.OpenStreamForWriteAsync()) 
       { 
        await stream.CopyToAsync(filestream); 
        return new Uri(string.Format("ms-appdata:///local/{0}.jpg", uniqueName), UriKind.Absolute); 
       } 
      } 
     } 
    } 
관련 문제