0

私のWindowsストア(別名Windows 8)アプリケーションは、デフォルトのグリッドアプリケーションテンプレートを使用してアイテムを表示します。そこにあるアイテムテンプレートには、テキスト情報がオーバーレイされた画像が含まれています。アプリケーションのサイズを小さくするために、すべてのアイテムの画像を保存するのではなく、画像が存在するWebサーバーへの絶対パス(http)を使用してURIを保存します。画像のURIにバインドするように標準テンプレートを変更しました(これを正しく機能させるには、URIを文字列に変換する必要がありました)。アプリケーションを起動するたびに、すべての画像がダウンロードされ、画像コントロールによって自動的に表示されます。

私が今欲しいのは、一度ダウンロードされた画像を自動的に保存し、ダウンロードされた画像のURIをローカルストレージを指すものに変更することです。ここで私は2つの問題にぶつかります:

  • から完全なItemTemplateをバインドすると、ImageOpenedイベントを発生させることができません。StandardStyles.xaml

これは私のからのバインディングですGroupedItemsPage.xaml

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

バインドされたテンプレートは、イベントを発生させるように変更されました(StandardStyles.xaml):

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

Image_ImageOpenedイベントハンドラーは分離コードファイル( `GroupedItemsPage.xaml.cs')で定義されていますが、起動することはありません。

    private void Image_ImageOpened(object sender, RoutedEventArgs e)
    {

    }
  • Imageフレームワーク要素のコンテンツをバイナリファイルとして保存する方法がわかりません。
4

1 に答える 1

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);
                }
            }
        }
    }
于 2012-09-16T02:22:28.653 に答える