Skip to content

Commit f3fdc5c

Browse files
committed
Improve DataGrid Performance by Removing Parent Grid
1 parent 615b778 commit f3fdc5c

File tree

2 files changed

+200
-55
lines changed

2 files changed

+200
-55
lines changed

FileLoader.cs

Lines changed: 197 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public ItemViewModel(string ViewPath, Page p)
182182

183183
tokenSource = new CancellationTokenSource();
184184
token = tokenSource.Token;
185-
GetItemsAsync(ViewPath, token);
185+
MemoryFriendlyGetItemsAsync(ViewPath, token);
186186

187187
if (pageName != "ClassicModePage")
188188
{
@@ -212,53 +212,55 @@ private async void DisplayConsentDialog()
212212
message.Commands.Add(new UICommand("Allow...", new UICommandInvokedHandler(Interaction.GrantAccessPermissionHandler)));
213213
await message.ShowAsync();
214214
}
215-
216-
217-
public async void GetItemsAsync(string path, CancellationToken token)
215+
string sort = "By_Name";
216+
SortEntry entry;
217+
public async void MemoryFriendlyGetItemsAsync(string path, CancellationToken token)
218218
{
219219
Stopwatch stopwatch = new Stopwatch();
220220
stopwatch.Start();
221221

222222
PUIP.Path = path;
223223
try
224224
{
225-
folder = await StorageFolder.GetFolderFromPathAsync(path); // Set location to the current directory specified in path
226-
folderList = await folder.GetFoldersAsync(); // Create a read-only list of all folders in location
227-
if (token.IsCancellationRequested == true)
225+
folder = await StorageFolder.GetFolderFromPathAsync(path);
226+
QueryOptions options = new QueryOptions()
228227
{
229-
return;
230-
}
231-
fileList = await folder.GetFilesAsync(); // Create a read-only list of all files in location
232-
NumOfFolders = folderList.Count; // How many folders are in the list
233-
NumOfFiles = fileList.Count; // How many files are in the list
234-
NumOfItems = NumOfFiles + NumOfFolders;
235-
NumItemsRead = 0;
228+
FolderDepth = FolderDepth.Shallow,
229+
IndexerOption = IndexerOption.UseIndexerWhenAvailable
230+
};
236231

237-
if (NumOfItems == 0)
232+
if (sort == "By_Name")
238233
{
239-
TextState.isVisible = Visibility.Visible;
234+
entry = new SortEntry()
235+
{
236+
AscendingOrder = true,
237+
PropertyName = "System.FileName"
238+
};
240239
}
240+
options.SortOrder.Add(entry);
241241

242-
PUIH.Header = "Loading " + NumOfItems + " items";
243-
ButtonText.buttonText = "Hide";
244-
245-
if (NumOfItems >= 250)
242+
uint index = 0;
243+
const uint step = 250;
244+
if (!folder.AreQueryOptionsSupported(options))
246245
{
247-
PVIS.isVisible = Visibility.Visible;
246+
options.SortOrder.Clear();
248247
}
249-
if (NumOfFolders > 0)
248+
249+
StorageFolderQueryResult folderQueryResult = folder.CreateFolderQueryWithOptions(options);
250+
IReadOnlyList<StorageFolder> folders = await folderQueryResult.GetFoldersAsync(index, step);
251+
while(folders.Count != 0)
250252
{
251-
foreach (StorageFolder fol in folderList)
253+
foreach(StorageFolder folder in folders)
252254
{
253-
if (token.IsCancellationRequested == true)
255+
if (token.IsCancellationRequested)
254256
{
255257
return;
256258
}
257-
int ProgressReported = (NumItemsRead * 100 / NumOfItems);
258-
UpdateProgUI(ProgressReported);
259-
gotFolName = fol.Name.ToString();
260-
gotFolDate = fol.DateCreated.ToString();
261-
gotFolPath = fol.Path.ToString();
259+
//int ProgressReported = (NumItemsRead * 100 / NumOfItems);
260+
//UpdateProgUI(ProgressReported);
261+
gotFolName = folder.Name.ToString();
262+
gotFolDate = folder.DateCreated.ToString();
263+
gotFolPath = folder.Path.ToString();
262264
gotFolType = "Folder";
263265
gotFolImg = Visibility.Visible;
264266
gotFileImgVis = Visibility.Collapsed;
@@ -272,48 +274,50 @@ public async void GetItemsAsync(string path, CancellationToken token)
272274
{
273275
FilesAndFolders.Add(new ListedItem() { ItemIndex = FilesAndFolders.Count, FileImg = null, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotFolName, FileDate = gotFolDate, FileExtension = gotFolType, FilePath = gotFolPath });
274276
}
275-
276-
277-
NumItemsRead++;
278277
}
279-
278+
index += step;
279+
folders = await folderQueryResult.GetFoldersAsync(index, step);
280280
}
281281

282-
if (NumOfFiles > 0)
282+
index = 0;
283+
StorageFileQueryResult fileQueryResult = folder.CreateFileQueryWithOptions(options);
284+
IReadOnlyList<StorageFile> files = await fileQueryResult.GetFilesAsync(index, step);
285+
286+
while (files.Count != 0)
283287
{
284-
foreach (StorageFile f in fileList)
288+
foreach (StorageFile file in files)
285289
{
286-
if (token.IsCancellationRequested == true)
290+
if (token.IsCancellationRequested)
287291
{
288292
return;
289293
}
290-
int ProgressReported = (NumItemsRead * 100 / NumOfItems);
291-
UpdateProgUI(ProgressReported);
292-
gotName = f.Name.ToString();
293-
gotDate = f.DateCreated.ToString(); // In the future, parse date to human readable format
294-
if (f.FileType.ToString() == ".exe")
294+
//int ProgressReported = (NumItemsRead * 100 / NumOfItems);
295+
//UpdateProgUI(ProgressReported);
296+
gotName = file.Name.ToString();
297+
gotDate = file.DateCreated.ToString(); // In the future, parse date to human readable format
298+
if (file.FileType.ToString() == ".exe")
295299
{
296300
gotType = "Executable";
297301
}
298302
else
299303
{
300-
gotType = f.DisplayType;
304+
gotType = file.DisplayType;
301305
}
302-
gotPath = f.Path.ToString();
306+
gotPath = file.Path.ToString();
303307
gotFolImg = Visibility.Collapsed;
304308
if (isPhotoAlbumMode == false)
305309
{
306310
const uint requestedSize = 20;
307311
const ThumbnailMode thumbnailMode = ThumbnailMode.ListView;
308312
const ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale;
309-
gotFileImg = await f.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);
313+
gotFileImg = await file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);
310314
}
311315
else
312316
{
313317
const uint requestedSize = 275;
314318
const ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView;
315319
const ThumbnailOptions thumbnailOptions = ThumbnailOptions.ResizeThumbnail;
316-
gotFileImg = await f.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);
320+
gotFileImg = await file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);
317321
}
318322

319323
BitmapImage icon = new BitmapImage();
@@ -331,18 +335,16 @@ public async void GetItemsAsync(string path, CancellationToken token)
331335
{
332336
FilesAndFolders.Add(new ListedItem() { FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath });
333337
}
334-
NumItemsRead++;
335338
}
336-
337-
339+
index += step;
340+
files = await fileQueryResult.GetFilesAsync(index, step);
338341
}
339342
if (pageName != "ClassicModePage")
340343
{
341344
PVIS.isVisible = Visibility.Collapsed;
342345
}
343346

344-
345-
}
347+
}
346348
catch (UnauthorizedAccessException)
347349
{
348350
DisplayConsentDialog();
@@ -356,8 +358,152 @@ public async void GetItemsAsync(string path, CancellationToken token)
356358
}
357359
stopwatch.Stop();
358360
Debug.WriteLine("Loading of: " + path + " completed in " + stopwatch.ElapsedMilliseconds + " Milliseconds.");
359-
360361
}
362+
363+
//public async void GetItemsAsync(string path, CancellationToken token)
364+
//{
365+
// Stopwatch stopwatch = new Stopwatch();
366+
// stopwatch.Start();
367+
368+
// PUIP.Path = path;
369+
// try
370+
// {
371+
// folder = await StorageFolder.GetFolderFromPathAsync(path); // Set location to the current directory specified in path
372+
// folderList = await folder.GetFoldersAsync(); // Create a read-only list of all folders in location
373+
// if (token.IsCancellationRequested == true)
374+
// {
375+
// return;
376+
// }
377+
// fileList = await folder.GetFilesAsync(); // Create a read-only list of all files in location
378+
// NumOfFolders = folderList.Count; // How many folders are in the list
379+
// NumOfFiles = fileList.Count; // How many files are in the list
380+
// NumOfItems = NumOfFiles + NumOfFolders;
381+
// NumItemsRead = 0;
382+
383+
// if (NumOfItems == 0)
384+
// {
385+
// TextState.isVisible = Visibility.Visible;
386+
// }
387+
388+
// PUIH.Header = "Loading " + NumOfItems + " items";
389+
// ButtonText.buttonText = "Hide";
390+
391+
// if (NumOfItems >= 250)
392+
// {
393+
// PVIS.isVisible = Visibility.Visible;
394+
// }
395+
// if (NumOfFolders > 0)
396+
// {
397+
// foreach (StorageFolder fol in folderList)
398+
// {
399+
// if (token.IsCancellationRequested == true)
400+
// {
401+
// return;
402+
// }
403+
// int ProgressReported = (NumItemsRead * 100 / NumOfItems);
404+
// UpdateProgUI(ProgressReported);
405+
// gotFolName = fol.Name.ToString();
406+
// gotFolDate = fol.DateCreated.ToString();
407+
// gotFolPath = fol.Path.ToString();
408+
// gotFolType = "Folder";
409+
// gotFolImg = Visibility.Visible;
410+
// gotFileImgVis = Visibility.Collapsed;
411+
412+
413+
// if (pageName == "ClassicModePage")
414+
// {
415+
// ClassicFolderList.Add(new Classic_ListedFolderItem() { FileName = gotFolName, FileDate = gotFolDate, FileExtension = gotFolType, FilePath = gotFolPath });
416+
// }
417+
// else
418+
// {
419+
// FilesAndFolders.Add(new ListedItem() { ItemIndex = FilesAndFolders.Count, FileImg = null, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotFolName, FileDate = gotFolDate, FileExtension = gotFolType, FilePath = gotFolPath });
420+
// }
421+
422+
423+
// NumItemsRead++;
424+
// }
425+
426+
// }
427+
428+
// if (NumOfFiles > 0)
429+
// {
430+
// foreach (StorageFile f in fileList)
431+
// {
432+
// if (token.IsCancellationRequested == true)
433+
// {
434+
// return;
435+
// }
436+
// int ProgressReported = (NumItemsRead * 100 / NumOfItems);
437+
// UpdateProgUI(ProgressReported);
438+
// gotName = f.Name.ToString();
439+
// gotDate = f.DateCreated.ToString(); // In the future, parse date to human readable format
440+
// if (f.FileType.ToString() == ".exe")
441+
// {
442+
// gotType = "Executable";
443+
// }
444+
// else
445+
// {
446+
// gotType = f.DisplayType;
447+
// }
448+
// gotPath = f.Path.ToString();
449+
// gotFolImg = Visibility.Collapsed;
450+
// if (isPhotoAlbumMode == false)
451+
// {
452+
// const uint requestedSize = 20;
453+
// const ThumbnailMode thumbnailMode = ThumbnailMode.ListView;
454+
// const ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale;
455+
// gotFileImg = await f.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);
456+
// }
457+
// else
458+
// {
459+
// const uint requestedSize = 275;
460+
// const ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView;
461+
// const ThumbnailOptions thumbnailOptions = ThumbnailOptions.ResizeThumbnail;
462+
// gotFileImg = await f.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);
463+
// }
464+
465+
// BitmapImage icon = new BitmapImage();
466+
// if (gotFileImg != null)
467+
// {
468+
// icon.SetSource(gotFileImg.CloneStream());
469+
// }
470+
// gotFileImgVis = Visibility.Visible;
471+
472+
// if (pageName == "ClassicModePage")
473+
// {
474+
// ClassicFileList.Add(new ListedItem() { FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath });
475+
// }
476+
// else
477+
// {
478+
// FilesAndFolders.Add(new ListedItem() { FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath });
479+
// }
480+
// NumItemsRead++;
481+
// }
482+
483+
484+
// }
485+
// if (pageName != "ClassicModePage")
486+
// {
487+
// PVIS.isVisible = Visibility.Collapsed;
488+
// }
489+
490+
491+
// }
492+
// catch (UnauthorizedAccessException)
493+
// {
494+
// DisplayConsentDialog();
495+
// }
496+
// catch (System.Runtime.InteropServices.COMException e)
497+
// {
498+
// Frame rootFrame = Window.Current.Content as Frame;
499+
// MessageDialog driveGone = new MessageDialog(e.Message, "Drive Not Found");
500+
// await driveGone.ShowAsync();
501+
// rootFrame.Navigate(typeof(MainPage), new SuppressNavigationTransitionInfo());
502+
// }
503+
// stopwatch.Stop();
504+
// Debug.WriteLine("Loading of: " + path + " completed in " + stopwatch.ElapsedMilliseconds + " Milliseconds.");
505+
506+
//}
361507

362508
public static ProgressPercentage progressPER = new ProgressPercentage();
363509

GenericFileBrowser.xaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@
8888
<ScrollViewer Padding="5" HorizontalScrollMode="Auto" HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Top">
8989
<TextBlock x:Name="VisiblePath" Text="{x:Bind Mode=TwoWay, Path=local:GenericFileBrowser.P.path, UpdateSourceTrigger=PropertyChanged}" FontFamily="Segoe UI Black" FontWeight="Bold" FontSize="32" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" VerticalAlignment="Top" Margin="0,0,0,0" Grid.Row="1"/>
9090
</ScrollViewer>
91-
<ProgressBar Visibility="{x:Bind local2:ItemViewModel.PVIS.isVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="{x:Bind local2:ItemViewModel.PROGRESSPER.prog, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch"/>
91+
<ProgressBar IsIndeterminate="True" Visibility="{x:Bind local2:ItemViewModel.PVIS.isVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="{x:Bind local2:ItemViewModel.PROGRESSPER.prog, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch"/>
9292
</StackPanel>
9393

9494

95-
<Grid Margin="24,0,0,0" Grid.Row="3" HorizontalAlignment="Left">
9695

97-
<controls:DataGrid CellEditEnded="AllView_CellEditEnded" FocusVisualPrimaryThickness="0" SelectionMode="Extended" IsDoubleTapEnabled="True" x:FieldModifier="public" x:Name="AllView" Drop="AllView_DropAsync" AutoGenerateColumns="False" CanDrag="False" AllowDrop="True" DragOver="AllView_DragOver" IsRightTapEnabled="True" CanUserReorderColumns="False" IsReadOnly="False" ItemsSource="{x:Bind local2:ItemViewModel.FilesAndFolders}" Margin="0,0,0,0" Padding="0, 0, 0, 0" HorizontalAlignment="Stretch">
96+
<controls:DataGrid Margin="24,0,0,0" Grid.Row="3" CellEditEnded="AllView_CellEditEnded" FocusVisualPrimaryThickness="0" SelectionMode="Extended" IsDoubleTapEnabled="True" x:FieldModifier="public" x:Name="AllView" Drop="AllView_DropAsync" AutoGenerateColumns="False" CanDrag="False" AllowDrop="True" DragOver="AllView_DragOver" IsRightTapEnabled="True" CanUserReorderColumns="False" IsReadOnly="False" ItemsSource="{x:Bind local2:ItemViewModel.FilesAndFolders}" HorizontalAlignment="Left">
9897

9998
<controls:DataGrid.Resources>
10099
<MenuFlyout x:Name="HeaderRightClickMenu" x:Key="HeaderRightClickFlyout">
@@ -190,7 +189,7 @@
190189

191190

192191

193-
</Grid>
192+
194193

195194
<ContentDialog BorderThickness="0" Name="AddDialog" PrimaryButtonText="Cancel" Loaded="ContentDialog_Loaded" Grid.RowSpan="4">
196195
<Frame Width="450" Name="AddDialogFrame"/>

0 commit comments

Comments
 (0)