加载图像存储在本地App文件夹windows 8 app中的ListView中

时间:2015-06-13 19:19:28

标签: windows-8 listviewitem

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

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

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

这有助于选择存储在app文件夹中的一个图像,但如何循环显示多个图像并将其添加到列表视图中。

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

您需要获取对正确的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);
}