C# 在Windows 8中,图像控件下载图像后,ro如何在本地存储图像?

C# 在Windows 8中,图像控件下载图像后,ro如何在本地存储图像?,c#,windows-8,local-storage,C#,Windows 8,Local Storage,我的Windows应用商店(又名Windows 8)应用程序使用默认的网格应用程序模板来显示项目。其中的项目模板包括一个带有重叠文本信息的图像。为了减小应用程序的大小,我不存储每个项目的图像,而是将带有绝对路径(http)的Uri保存到图像所在的Web服务器。我修改了标准模板以绑定到图像Uri(我必须将Uri转换为字符串才能正常工作),现在每当我启动应用程序时,所有图像都会被下载并由图像控件自动显示 我现在想要的是自动保存曾经下载的图像,并将下载图像的URI修改为指向本地存储的URI。在这里,我

我的Windows应用商店(又名Windows 8)应用程序使用默认的网格应用程序模板来显示项目。其中的项目模板包括一个带有重叠文本信息的图像。为了减小应用程序的大小,我不存储每个项目的图像,而是将带有绝对路径(http)的Uri保存到图像所在的Web服务器。我修改了标准模板以绑定到图像Uri(我必须将Uri转换为字符串才能正常工作),现在每当我启动应用程序时,所有图像都会被下载并由图像控件自动显示

我现在想要的是自动保存曾经下载的图像,并将下载图像的URI修改为指向本地存储的URI。在这里,我遇到了两个问题:

  • 如果从
    StandardStyles.xaml
这是我的
GroupedItemsPage.xaml中的绑定:

    <GridView
        x:Name="itemGridView"
        ItemTemplate="{StaticResource Standard250x250ItemTemplate}">
<DataTemplate x:Key="Standard250x250ItemTemplate">
            <Image Source="{Binding ImageUri}" ImageOpened="Image_ImageOpened"/>
</DataTemplate>
Image\u ImageOpened
事件处理程序在代码隐藏文件(`GroupedItemsPage.xaml.cs')中定义,但从不触发:

    private void Image_ImageOpened(object sender, RoutedEventArgs e)
    {

    }
  • 我不知道如何将Image framework元素的内容存储为二进制文件

    • 我还必须在本地复制一些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);
                      }
                  }
              }
          }
      
      //
      ///将映像从internet(http协议)本地复制到AppData LocalFolder。这是一些方法使用的
      ///(如SecondaryTile构造函数)不支持通过http引用图像,但可以使用
      ///ms appdata协议。
      /// 
      ///internet上图像的路径(URI)
      ///本地文件的唯一名称
      ///已在本地复制的图像的路径
      专用异步任务GetLocalImageAsync(字符串internetUri,字符串uniqueName)
      {
      if(string.IsNullOrEmpty(internetUri))
      {
      返回null;
      }
      使用(var response=wait HttpWebRequest.CreateHttp(internetUri.GetResponseAsync())
      {
      使用(var stream=response.GetResponseStream())
      {
      var desiredName=string.Format(“{0}.jpg”,uniqueName);
      var file=waiting ApplicationData.Current.LocalFolder.CreateFileAsync(需要的名称,creationCollectionOption.ReplaceExisting);
      使用(var filestream=await file.OpenStreamForWriteAsync())
      {
      等待stream.CopyToAsync(filestream);
      返回新的Uri(string.Format(“ms-appdata:///local/{0}.jpg“,uniqueName),UriKind.Absolute);
      }
      }
      }
      }
      
      我还必须在本地复制一些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);
                      }
                  }
              }
          }
      
      //
      ///将映像从internet(http协议)本地复制到AppData LocalFolder。这是一些方法使用的
      ///(如SecondaryTile构造函数)不支持通过http引用图像,但可以使用
      ///ms appdata协议。
      /// 
      ///internet上图像的路径(URI)
      ///本地文件的唯一名称
      ///已在本地复制的图像的路径
      专用异步任务GetLocalImageAsync(字符串internetUri,字符串uniqueName)
      {
      if(string.IsNullOrEmpty(internetUri))
      {
      返回null;
      }
      使用(var response=wait HttpWebRequest.CreateHttp(internetUri.GetResponseAsync())
      {
      使用(var stream=response.GetResponseStream())
      {
      var desiredName=string.Format(“{0}.jpg”,uniqueName);
      var file=wait ApplicationData.Current.LocalFolder.CreateFileAsync(desiredName,CreationCollisionOption.ReplaceExisting);
      使用(var filestream=await file.OpenStreamForWriteAsync())
      {
      等待stream.CopyToAsync(filestream);
      返回新的Uri(string.Format(“ms-appdata:///local/{0}.jpg“,uniqueName),UriKind.Absolute);
      }
      }
      }
      }
      
      @mydogisbox是,如果有internet连接,所有图像都会显示得非常好。@mydogisbox是,如果有internet连接,所有图像都会显示得非常好。