我正在尝试将一个装满图片的文件夹加载到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);
}
除了最初的问题,这是一种有效的方法吗? 提前致谢
答案 0 :(得分: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;
}