MAUI中隐藏飞出菜单按钮及主页导航显示菜单的方法
MAUI向导页面导航问题解决方案
需求1:向导页面显示时隐藏飞出菜单按钮
通过设置页面的FlyoutBehavior为Disabled即可隐藏汉堡按钮,有两种实现方式:
方式1:在AppShell配置中直接设置
修改AppShell.xaml里的ShellContent节点,添加Shell.FlyoutBehavior附加属性:
<ShellContent Shell.NavBarIsVisible="true" Shell.TabBarIsVisible="false" Shell.FlyoutBehavior="Disabled" Title="Enrolamiento" ContentTemplate="{DataTemplate views:EnrolamientoPage}" Route="EnrolamientoPage"> <ShellContent.Icon> <FontImageSource FontFamily="FontAwesomeSolid" Glyph="{x:Static helpers:FontAwesomeIcons.User}" Color="Maroon" Size="50"/> </ShellContent.Icon> </ShellContent>
方式2:在向导页面代码后台设置
打开EnrolamientoPage.xaml.cs,在构造函数中添加代码:
public EnrolamientoPage() { InitializeComponent(); Shell.SetFlyoutBehavior(this, FlyoutBehavior.Disabled); }
需求2:导航到MainPage后显示飞出菜单而非返回箭头
默认导航会把MainPage压入现有导航栈,导致页面处于栈中而非根节点,因此显示返回箭头。只需使用绝对路由清空导航栈,让MainPage回到根节点状态:
修改导航代码为:
// 替换原导航代码,使用绝对路由跳转 await Shell.Current.GoToAsync($"//{nameof(MainPage)}");
解释:以//开头的绝对路由会直接跳转到指定的根路由页面,同时清空之前的导航栈,此时MainPage作为根页面显示,导航栏会自动显示飞出菜单按钮。
内容的提问来源于stack exchange,提问作者jstuardo
相关产品推荐
相关产品推荐

