【问题标题】:Empty items on Flipview using Itemsource使用 Itemssource 在 Flipview 上清空项目
【发布时间】:2018-04-22 19:00:25
【问题描述】:

我正在尝试将一个充满图像的文件夹加载到 Flipview 中,如果该文件夹位于项目本身内,则没有问题,但如果我尝试使用不同的位置,Fliview 将加载空项目。

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>

代码背后:

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

除了最初的问题,这是一种有效的方法吗? 提前致谢

【问题讨论】:

    标签: c# uwp flipview


    【解决方案1】:

    一般来说,要获取所有的图片文件,我们应该添加图片FileTypeFilter,比如.png.jpg,然后使用StorageFolder.GetFilesAsync的方法来获取所有的图片文件,然后我们就可以显示所有的图片文件了。图片。

    下面是代码示例:

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

    【讨论】:

    • 感谢您的回答,但该方法将每个图像加载到内存中,然后将其添加到 FlipView(我对其进行了测试,没有使用缩略图,并且在 10 个相对较大的图像中使用了超过 500 mb)。
    • @Micheleuno 首先我要指出的是Path in UWP apps和我们的应用可以直接访问应用安装目录path2中的文件,这就是pathpath2。另外,一次显示很多大图,又不想消耗太多内存资源,似乎不太合理。请参阅主题Optimize image resources
    • 我对这种方法非常好奇(我在某处的指南上看到过)。目前我正在做一些与您的示例类似的事情,但在 Flipview 的每个“SelectionChanged”上添加一个 BitmapImage,从而减少使用的初始 Ram 数量。我会读最后一篇文章,看起来很有趣,
    猜你喜欢
    • 1970-01-01
    • 2010-12-08
    • 2012-07-24
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2013-08-08
    • 2012-08-02
    相关资源
    最近更新 更多