Empty items on Flipview using Itemsource

81 Views Asked by At

I am trying to load a folder full of images into a Flipview, if that folder is inside the project itself there is no problem, but if i try with a different location, the Fliview is loaded with empty items.

XAML:

<FlipView x:Name="FVtest" HorizontalAlignment="Center"  VerticalAlignment="Center" Width="750" Height="750">
    <FlipView.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}" Stretch="UniformToFill" />               
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>

Code Behind:

public async void Funcion (){

        var picker = new Windows.Storage.Pickers.FolderPicker()
        {
            ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads,
            SettingsIdentifier = "Setting"
        };
        picker.FileTypeFilter.Add("*");
        Windows.Storage.StorageFolder folder = await picker.PickSingleFolderAsync();
        StorageApplicationPermissions.FutureAccessList.AddOrReplace(folder.Name, folder);

        String path = folder.Path; //getting the path of a folder selected by the user
        String path2 = Directory.GetCurrentDirectory() + @"\Images"; //This one works

        FVtest.ItemsSource = Directory.GetFiles(path2); 
    }

Besides the initial problem, is this an efficient approach? Thanks in advance

1

There are 1 best solutions below

3
Breeze Liu - MSFT On

Generally, to get all the image files, we should add the image FileTypeFilter, such as .png and .jpg and use the StorageFolder.GetFilesAsync Method to get all the image files, then we can show all the images.

Here is the code example:

public async void Funcion()
{
    var picker = new Windows.Storage.Pickers.FolderPicker()
    {
        ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
        SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads,
        SettingsIdentifier = "Setting"
    };
    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".png");

    Windows.Storage.StorageFolder folder = await picker.PickSingleFolderAsync();
    StorageApplicationPermissions.FutureAccessList.AddOrReplace(folder.Name, folder);
    ObservableCollection<BitmapImage> sourceImage = new ObservableCollection<BitmapImage>();
    IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();
    if (files != null)
    {
        foreach (var file in files)
        {
            var thumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView);
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(thumbnail);
            sourceImage.Add(bitmap);
        }
    }
    //String path = folder.Path; //getting the path of a folder selected by the user
    //String path2 = Directory.GetCurrentDirectory() + @"\Images"; //This one works

    FVtest.ItemsSource = sourceImage;
}