如何RO形象店在当地一旦被Image控件在Windows 8下载? [英] How ro store image locally once it was downloaded by Image control in Windows 8?

查看:170
本文介绍了如何RO形象店在当地一旦被Image控件在Windows 8下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Windows应用商店(又名的Windows 8)应用程序使用默认电网应用程序模板,显示的项目。该项目模板中,有包括具有覆盖文本信息的图像。为了减少应用程序的大小,我不存储图像的每一个项目,而不是我一个URI以绝对路径(HTTP)保存到图片所在的网络服务器。我修改绑定到图像开放的标准模板(我不得不到URI转换成字符串为此才能正常工作),现在每当我开始所有的图像下载和图像控制系统自动显示的应用程序。

My Windows Store (aka Windows 8) application uses the default Grid Application template to show the items. The item template there includes an image with overlaid text information. To diminish the size of application I do not store the images for every item, instead I save an Uri with absolute path (http) to the webserver where the image resides. I modified the standard template to bind to the image Uri (I had to convert the Uri to string for this to work properly) and now whenever I start the application all the images are downloaded and shown automatically by the Image control.

我现在想的是自动保存这些曾经被下载的图像和修改下载的图像的URI来那些指向本地存储。在这里,我碰到两个问题:

What I now want is to automatically save the images which were once downloaded and modify the Uris of the downloaded images to those pointing to the local storage. Here I bump into two problems:


  • 我不能让ImageOpened事件,火灾,如果我从<$ C绑定的ItemTemplate完成$ C> StandardStyles.xaml

  • I can't get the ImageOpened event to fire if I bind the complete ItemTemplate from the 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'),但从来没有火灾:

The Image_ImageOpened event handler is defined in the code-behind file (`GroupedItemsPage.xaml.cs'), but never fires:

    private void Image_ImageOpened(object sender, RoutedEventArgs e)
    {

    }




  • 我不知道如何存储图像框架元素作为一个二进制文件的内容。

  • 推荐答案

    我也不得不在本地复制某些HTTP图像;这里是我工作的代码。你应该调用此方法internetURI =HTTP://的地方,你的图像文件,是和映像的唯一名称。这将图像复制到应用程序数据的存储LocalFolder,然后返回路径,您可以使用您绑定新的本地形象。希望这可以帮助!

    I also had to copy some http images locally; here is my working code. You should call this method with internetURI = "http://wherever-your-image-file-is" and a unique name for the image. It will copy the image to AppData's LocalFolder storage, and then it returns the path to the new local image which you can use for your binding. Hope this helps!

        /// <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);
                    }
                }
            }
        }
    

    这篇关于如何RO形象店在当地一旦被Image控件在Windows 8下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆