mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
CmdPal: Add null check before caching view model for item (#45815)
This commit is contained in:
@@ -227,32 +227,44 @@ public partial class ListViewModel : PageViewModel, IDisposable
|
|||||||
var reused = 0;
|
var reused = 0;
|
||||||
foreach (var item in newItems)
|
foreach (var item in newItems)
|
||||||
{
|
{
|
||||||
// Check for cancellation during item processing
|
try
|
||||||
if (cancellationToken.IsCancellationRequested)
|
|
||||||
{
|
{
|
||||||
return;
|
if (item is null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for cancellation during item processing
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_vmCache.TryGetValue(item, out var existing))
|
||||||
|
{
|
||||||
|
existing.LayoutShowsTitle = showsTitle;
|
||||||
|
existing.LayoutShowsSubtitle = showsSubtitle;
|
||||||
|
newViewModels.Add(existing);
|
||||||
|
reused++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var viewModel = new ListItemViewModel(item, new(this), _contextMenuFactory);
|
||||||
|
|
||||||
|
// If an item fails to load, silently ignore it.
|
||||||
|
if (viewModel.SafeFastInit())
|
||||||
|
{
|
||||||
|
viewModel.LayoutShowsTitle = showsTitle;
|
||||||
|
viewModel.LayoutShowsSubtitle = showsSubtitle;
|
||||||
|
|
||||||
|
_vmCache[item] = viewModel;
|
||||||
|
newViewModels.Add(viewModel);
|
||||||
|
created++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
if (_vmCache.TryGetValue(item, out var existing))
|
|
||||||
{
|
{
|
||||||
existing.LayoutShowsTitle = showsTitle;
|
CoreLogger.LogError("Failed to load item:\n", ex + ToString());
|
||||||
existing.LayoutShowsSubtitle = showsSubtitle;
|
|
||||||
newViewModels.Add(existing);
|
|
||||||
reused++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var viewModel = new ListItemViewModel(item, new(this), _contextMenuFactory);
|
|
||||||
|
|
||||||
// If an item fails to load, silently ignore it.
|
|
||||||
if (viewModel.SafeFastInit())
|
|
||||||
{
|
|
||||||
viewModel.LayoutShowsTitle = showsTitle;
|
|
||||||
viewModel.LayoutShowsSubtitle = showsSubtitle;
|
|
||||||
|
|
||||||
_vmCache[item] = viewModel;
|
|
||||||
newViewModels.Add(viewModel);
|
|
||||||
created++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user