【问题标题】:Load Images In a ListView stored in local App folder windows 8 app在存储在本地 App 文件夹 windows 8 app 中的 ListView 中加载图像
【发布时间】:2015-06-14 04:22:37
【问题描述】:

我有一个单击按钮,我必须在列表视图中填充多个图像。我看过一些类似的stackoverflow帖子,但没有帮助。 我想从我的应用程序本地文件夹中填充图像,然后将其设置为我的列表视图的来源。 如何从文件夹中选择多个图像文件并将其添加到列表视图的任何示例。 就像例如

var uri = new Windows.Foundation.Uri('ms-appx:///images/logo.png');

var 文件 = Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri);`

这将有助于选择存储在 app 文件夹中的一张图片,但如何遍历多张图片并将其添加到列表视图中。

此外,我还添加了另一个网格。当我从列表视图中选择图像时,它应该加载到网格中。所以在选择事件上改变了我应该如何抓取图像的文件路径或在该网格中加载选定的图像。

提前致谢。

【问题讨论】:

    标签: windows-8 listviewitem


    【解决方案1】:

    您需要获取对正确 StorageFolder 的引用,然后您可以遍历 StorageFiles。您可以从 StorageFile 创建一个对象。由于这听起来不像您在使用 MVVM,所以我将在示例后面显示一个快速代码。

    //helper class to store the URI of the image and the file name
    public class MyImage
    {
        public MyImage(StorageFile file)
        {
            this.Uri = new Uri(string.Format("ms-appx:///images/{0}", file.Name);
            this.Name = file.Name;
        }
    
        public Uri Uri { get; set; }
        public string Name { get; set; }
    }
    
    //Called from your button click event
    private async Task LoadImagesAsync()
    {
        //This get's your installation root folder, then traverse into the images folder
        //In a URI, this is equivalent to "ms-appx:///images"
        var imagesFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("images");
    
        var files = await imagesFolder.GetFilesAsync();
    
        //may want to filter your image types here
    
        var itemsSource = files.Select(x => new MyImage(x));
    
        //assume your ListView has a x:Name attribute of "MyList"
    
        this.MyList.ItemsSource = itemsSource;
    }
    
    //called from your SelectionChanged event from your ListView
    private void SetImage()
    {
        var myImage = this.MyList.SelectedItem as MyImage;
    
        //assume you have an Image in your XAML with an x:Name of "GridImage"
    
        this.GridImage.Source = new BitmapImage(myImage.Uri);
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 2021-10-29
      • 1970-01-01
      • 2014-10-25
      相关资源
      最近更新 更多