important: keep this pagecontext alive for the duration

if you don't, then eventually, we'll GC this object, and all objects
that took a weak ref on it will throw.
This commit is contained in:
Mike Griese
2026-02-20 10:15:43 -06:00
parent b326300537
commit f748e3ed5f
3 changed files with 6 additions and 3 deletions

View File

@@ -51,12 +51,15 @@ public sealed class CommandProviderWrapper : ICommandProviderContext
public bool SupportsPinning { get; private set; }
public TopLevelItemPageContext TopLevelPageContext { get; }
public CommandProviderWrapper(ICommandProvider provider, TaskScheduler mainThread)
{
// This ctor is only used for in-proc builtin commands. So the Unsafe!
// calls are pretty dang safe actually.
_commandProvider = new(provider);
_taskScheduler = mainThread;
TopLevelPageContext = new TopLevelItemPageContext(this, _taskScheduler);
// Hook the extension back into us
ExtensionHost = new CommandPaletteHost(provider);
@@ -81,6 +84,7 @@ public sealed class CommandProviderWrapper : ICommandProviderContext
{
_taskScheduler = mainThread;
_commandProviderCache = commandProviderCache;
TopLevelPageContext = new TopLevelItemPageContext(this, _taskScheduler);
Extension = extension;
ExtensionHost = new CommandPaletteHost(extension);

View File

@@ -103,8 +103,7 @@ public partial class TopLevelCommandManager : ObservableObject,
// May be called from a background thread
private async Task<IEnumerable<TopLevelViewModel>> LoadTopLevelCommandsFromProvider(CommandProviderWrapper commandProvider)
{
TopLevelItemPageContext pageContext = new(commandProvider, _taskScheduler);
WeakReference<IPageContext> weak = new(pageContext);
WeakReference<IPageContext> weak = new(commandProvider.TopLevelPageContext);
await commandProvider.LoadTopLevelCommands(_serviceProvider, weak);

View File

@@ -11,7 +11,7 @@ namespace Microsoft.CmdPal.UI.ViewModels;
/// Used as the PageContext for top-level items. Top level items are displayed
/// on the MainListPage, which _we_ own. We need to have a placeholder page
/// context for each provider that still connects those top-level items to the
/// CommandProvider they came from.
/// CommandProvider they came from.
/// </summary>
public partial class TopLevelItemPageContext : IPageContext
{