From 8c1e4f16fe0b0dba355a81f6210d9f33cc49d1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pol=C3=A1=C5=A1ek?= Date: Fri, 27 Feb 2026 18:20:19 +0100 Subject: [PATCH] CmdPal: Add null check before caching view model for item (#45815) --- .../ListViewModel.cs | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ListViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ListViewModel.cs index 8953312c7b..64db00c3e1 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ListViewModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ListViewModel.cs @@ -227,32 +227,44 @@ public partial class ListViewModel : PageViewModel, IDisposable var reused = 0; foreach (var item in newItems) { - // Check for cancellation during item processing - if (cancellationToken.IsCancellationRequested) + try { - 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++; + } } - - if (_vmCache.TryGetValue(item, out var existing)) + catch (Exception ex) { - 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++; + CoreLogger.LogError("Failed to load item:\n", ex + ToString()); } }