英文:
.NET MAUI Shell: How can `ShellContent` page be added to the navigation stack to enable "navigate back" behavior?
问题
我在MAUI Shell XAML中有几个页面:
<ShellContent
Title="仪表板"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage">
<ShellContent.Icon>
<FontImageSource Glyph="" FontFamily="AwesomeSolid" Color="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource Gray100}}" />
</ShellContent.Icon>
</ShellContent>
<ShellContent
Title="Ot"
ContentTemplate="{DataTemplate local:OtherPage}"
Route="OtherPage">
<ShellContent.Icon>
<FontImageSource Glyph="" FontFamily="AwesomeSolid" Color="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource Gray100}}" />
</ShellContent.Icon>
</ShellContent>
Shell导航正常工作,但如果用户在"OtherPage"上点击"返回",应用程序将不会将用户返回到主页面。此外,从Shell Flyout导航不会在"OtherPage"上添加返回按钮。
如果我在"OtherPage"上放置一个按钮并尝试查看导航堆栈,似乎它不记录先前的页面:
private async void Button_Clicked(object sender, EventArgs e)
{
Console.WriteLine(Shell.Current.Navigation.NavigationStack.Count); // 打印 1
await Shell.Current.Navigation.PopAsync(); // 不起作用
await Shell.Current.GoToAsync(".."); // 不起作用
await Shell.Current.GoToAsync("///MainPage"); // 导航到MainPage
}
从最后一个调用中,我可以看到导航正在工作,但从Shell导航不会将页面添加到导航堆栈。
如何实现以下行为:
- 应用程序从"MainPage"开始
- 用户通过Shell Flyout导航到"OtherPage"
- 用户在"OtherPage"上点击"返回"按钮并返回到"MainPage"
英文:
I have several page in MAUI Shell XAML:
<ShellContent
Title="Dashboard"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage">
<ShellContent.Icon>
<FontImageSource Glyph="&#xf0db;" FontFamily="AwesomeSolid" Color="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource Gray100}}" />
</ShellContent.Icon>
</ShellContent>
<ShellContent
Title="Ot"
ContentTemplate="{DataTemplate local:OtherPage}"
Route="OtherPage">
<ShellContent.Icon>
<FontImageSource Glyph="&#xe52f;" FontFamily="AwesomeSolid" Color="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource Gray100}}" />
</ShellContent.Icon>
</ShellContent>
Shell navigation works fine, but if user taps "back" on OtherPage
the application will not return the user back to the main page. Also, navigation from Shell Flyout does not add a back button to the OtherPage
.
If I put a button on OtherPage
and try to explore navigation stack it seems like it doesn't record previous page:
private async void Button_Clicked(object sender, EventArgs e)
{
Console.WriteLine(Shell.Current.Navigation.NavigationStack.Count); // prints 1
await Shell.Current.Navigation.PopAsync(); // doesn't work
await Shell.Current.GoToAsync(".."); // doesn't work
await Shell.Current.GoToAsync("///MainPage"); // navigates to MainPage
}
From the last call I can see that navigation is working, but navigating from Shell doesn't add page to the navigation stack.
How do I achieve the behavior that:
- Application starts on
MainPage
- User navigates to
OtherPage
via Shell Flyout - User taps "back" button while on
OtherPage
and returns toMainPage
答案1
得分: 1
Shell.Xaml中的Shell内容将默认设置为导航堆栈中的根页面。换句话说,当您通过Shell Flyout导航到一个页面时,该页面将成为根页面。
当您导航到一个未添加到Shell作为flyoutitem的页面时,返回按钮将仅显示。因此,当您在两个页面之间导航时,OtherPage和MainPage都不会显示返回按钮。使用Shell.Current.GoToAsync
在两个页面之间导航与使用Shell Flyout相同。
因此,您可以删除其中一个ShellContent,并只使用Shell.Current.GoToAsync
在两个页面之间导航,或者只使用Shell Flyout。
更新
在OtherPage的xaml.cs中:
protected override bool OnBackButtonPressed()
{
await Shell.Current.GoToAsync("///MainPage");
return true;
}
英文:
The shell content in the Shell.Xaml will be default set as the root page in the navigation stack. In other words, when you navigates to a page via Shell Flyout, that page will be a rootpage.
And the back button will show only when you go to a page which is not added into the Shell as a flyoutitem. So both of the OtherPage and the MainPage will not show the back button when you navigation between the two pages. You navigate between the two pages with the Shell.Current.GoToAsync
is same as using the Shell Flyout.
So you can remove one of the ShellContent and only use the Shell.Current.GoToAsync
to navigate between the two page or only use the Shell Flyout.
Update
In the OtherPage's xaml.cs:
protected override bool OnBackButtonPressed()
{
await Shell.Current.GoToAsync("///MainPage");
return true;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论