Implement push args.

This commit is contained in:
Yu Leng (from Dev Box)
2025-03-24 15:33:38 +08:00
parent 60ee9bda0d
commit 0ff400f71e
4 changed files with 65 additions and 49 deletions

View File

@@ -22,25 +22,19 @@ public sealed partial class SampleGoToPage : ListPage
{
var goBackArgs = new GoToPageArgs
{
PageId = string.Empty,
PageId = "com.microsoft.SamplePages.LandingPage",
NavigationMode = NavigationMode.GoBack,
};
var goHomeArgs = new GoToPageArgs
{
PageId = "com.microsoft.SamplePages",
PageId = "com.microsoft.SamplePages.LandingPage",
NavigationMode = NavigationMode.GoHome,
};
var goToCalculatorHomeArgs = new GoToPageArgs
var pushLandingPageArgs = new GoToPageArgs
{
PageId = "com.microsoft.cmdpal.calculator",
NavigationMode = NavigationMode.GoHome,
};
var pushArgs = new GoToPageArgs
{
PageId = "com.microsoft.SamplePages",
PageId = "com.microsoft.SamplePages.LandingPage",
NavigationMode = NavigationMode.Push,
};
@@ -52,7 +46,7 @@ public sealed partial class SampleGoToPage : ListPage
Result = CommandResult.GoToPage(goBackArgs),
})
{
Title = "Go to back page.",
Title = "Go back and then go to landing page.",
Icon = new IconInfo("\uEA37"),
},
new ListItem(
@@ -61,27 +55,18 @@ public sealed partial class SampleGoToPage : ListPage
Result = CommandResult.GoToPage(goHomeArgs),
})
{
Title = "Go to Home page.",
Title = "Go back to home page and then go to landing page.",
Icon = new IconInfo("\uEA37"),
},
new ListItem(
new AnonymousCommand(() => { })
{
Result = CommandResult.GoToPage(goToCalculatorHomeArgs),
Result = CommandResult.GoToPage(pushLandingPageArgs),
})
{
Title = "Go to Calculator Home page.",
Title = "Push landing page.",
Icon = new IconInfo("\uEA37"),
},
new ListItem(
new AnonymousCommand(() => { })
{
Result = CommandResult.GoToPage(pushArgs),
})
{
Title = "Push current page.",
Icon = new IconInfo("\uEA37"),
}
];
}
}

View File

@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
namespace SamplePagesExtension;
public sealed partial class SampleLandingPage : ListPage
{
public SampleLandingPage()
{
Icon = new IconInfo("\uEA37");
Name = "This is a sample landing page to land for GotoPage call.";
Id = "com.microsoft.SamplePages.LandingPage";
}
public override IListItem[] GetItems()
{
return [
new ListItem(new NoOpCommand())
{
Title = "This is a basic item in the list",
Subtitle = "I don't do anything though",
},
new ListItem(new NoOpCommand())
{
Title = "This is a sample landing page to land for GotoPage call.",
Subtitle = "I don't do anything though",
},
];
}
}

View File

@@ -21,6 +21,11 @@ public partial class SamplePagesCommandsProvider : CommandProvider
Title = "Sample Pages",
Subtitle = "View example commands",
},
new CommandItem(new SampleLandingPage())
{
Title = "Sample Landing Pages",
Subtitle = "Sample Landing pages example for goto page",
},
];
public override ICommandItem[] TopLevelCommands()

View File

@@ -272,53 +272,45 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
var pageID = vm.PageId;
var navigationMode = vm.NavigationMode;
if (string.IsNullOrEmpty(pageID) && navigationMode != NavigationMode.GoBack)
if (string.IsNullOrEmpty(pageID))
{
_toast.ShowToast("Invalid page id");
return;
}
var tlcManager = App.Current.Services.GetService<TopLevelCommandManager>()!;
var toplevelCommand = tlcManager.LookupCommand(pageID);
if (toplevelCommand == null)
{
// TODO: Consider to show a toast?
return;
}
switch (navigationMode)
{
case NavigationMode.Push:
// TODO: Implement push
break;
case NavigationMode.GoHome:
var tlcManager = App.Current.Services.GetService<TopLevelCommandManager>()!;
var toplevelCommand = tlcManager.LookupCommand(pageID);
if (toplevelCommand == null)
{
break;
}
/* Stack looks like this:
* 0: Main page
* 1: Top level command page (your extensions first page)
* 2: One of your extension pages listed in the top level command page results
* 3: ...
* ...
*/
// So, we need to clean up existing stack frame. And push to the next top level page.
while (RootFrame.CanGoBack)
{
GoBack(false, false);
}
RootFrame.ForwardStack.Clear();
var msg = new PerformCommandMessage(toplevelCommand) { WithAnimation = true };
WeakReferenceMessenger.Default.Send<PerformCommandMessage>(msg);
break;
case NavigationMode.GoBack:
GoBack();
if (RootFrame.CanGoBack)
{
GoBack(false, false);
}
break;
}
var msg = new PerformCommandMessage(toplevelCommand) { WithAnimation = true };
WeakReferenceMessenger.Default.Send<PerformCommandMessage>(msg);
return;
}