我正在使用带有导航栏的 Template Studio 开发 WinUI 3 应用程序。
INavigationService
有一个 NavigateTo
方法,我可以使用它在 Login
和 SignUp
等自定义页面之间导航。但是,我遇到了一个问题,成功登录后无法导航到 ShellPage
。
具体来说,以下代码无法按预期工作:
private readonly INavigationService _navigationService;
private UIElement? _shell;
public LoginViewModel(IAuthService authService, INavigationService navigationService)
{
_authService = authService;
_navigationService = navigationService;
}
if (loginSuccessful)
{
// Not working in this context
_navigationService.NavigateTo(typeof(ShellViewModel).FullName!);
// Works but doesn't show full content of ShellPage (doesn't automatically activate BlankPage)
var shell = App.GetService<ShellPage>();
App.MainWindow.Content = shell ?? (UIElement)new Frame();
}
有趣的是,如果我将
NavigateTo
与像 SignUp
这样的自定义页面一起使用,它工作得很好:
public void NavigateToSignUp()
{
_navigationService.NavigateTo(typeof(SignUpViewModel).FullName!);
}
我正在尝试使用另一种方式,例如:
// Works but doesn't show full content of ShellPage (doesn't automatically activate BlankPage)
var shell = App.GetService<ShellPage>();
App.MainWindow.Content = shell ?? (UIElement)new Frame();
然后我有: 在此输入图片描述 但我预计: 在此输入图片描述 (ShellPage 包括 AccountPage) 登录后如何成功导航到
ShellPage
并确保ShellPage
显示其完整内容(包括自动激活默认的Account
)?
Template Studio 有一个名为
PageService
的类。 IIRC,您需要在其构造函数中添加导航目标。
例如:
PageService.cs
public PageService()
{
Configure<MainViewModel, MainPage>();
Configure<WebViewViewModel, WebViewPage>();
Configure<ListDetailsViewModel, ListDetailsPage>();
Configure<ContentGridViewModel, ContentGridPage>();
Configure<ContentGridDetailViewModel, ContentGridDetailPage>();
Configure<DataGridViewModel, DataGridPage>();
Configure<SettingsViewModel, SettingsPage>();
// Your pages here:
Configure<ShellViewModel, ShellPage>();
}