Implement simple file picker poc for UWP

This commit is contained in:
Denis-Cosmin Nutiu 2022-02-01 22:27:39 +02:00
parent d0e2231fb5
commit 2c40a1412b
2 changed files with 36 additions and 11 deletions

View file

@ -9,6 +9,13 @@
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<StackPanel x:Name="MainStackPanel">
<ListView x:Name="FilesListView">
<TextBlock>test 1</TextBlock>
<TextBlock>test 2</TextBlock>
<TextBlock>test 3</TextBlock>
<TextBlock>test 4</TextBlock>
</ListView>
</StackPanel>
</Grid>
</Page>

View file

@ -1,17 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
@ -25,6 +15,34 @@ namespace UwpApplication
public MainPage()
{
this.InitializeComponent();
Console.WriteLine("Main Page");
this.Loaded += LoadedHandler;
}
private async void LoadedHandler(object sender, RoutedEventArgs e)
{
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
// Application now has read/write access to all contents in the picked folder
// (including other sub-folder contents)
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
List<string> fileNames = new List<string>();
var folderItems = await folder.GetItemsAsync();
foreach (var item in folderItems)
{
if (item.Attributes == Windows.Storage.FileAttributes.Archive || item.Attributes == Windows.Storage.FileAttributes.Normal)
{
fileNames.Add(item.Name);
}
}
FilesListView.ItemsSource = fileNames;
}
}
}
}