您可以SideMenuViewPage
从Xamarin.CommunityToolkit 包中使用,它已经可用但尚未记录。
您可以在SideMenuViewPage.xaml中找到以下代码的完整示例
样本
<xct:SideMenuView x:Name="SideMenuView">
<!-- MainView -->
<StackLayout BackgroundColor="Gold">
<Label Text="This is the Main view" VerticalOptions="Center" HorizontalOptions="Center"/>
</StackLayout>
<!-- LeftMenu -->
<StackLayout
xct:SideMenuView.Position="LeftMenu"
xct:SideMenuView.MenuWidthPercentage=".5"
xct:SideMenuView.MainViewScaleFactor=".95"
BackgroundColor="Orange">
<Label Text="This is the left menu view"/>
</StackLayout>
<!-- RightMenu -->
<StackLayout
xct:SideMenuView.Position="RightMenu"
xct:SideMenuView.MenuWidthPercentage=".3"
xct:SideMenuView.MenuGestureEnabled="False"
BackgroundColor="LightGreen">
<Label Text="This is the Right menu view"/>
</StackLayout>
</xct:SideMenuView>
可用属性
财产 |
描述 |
MenuWidthPercentage |
设置菜单相对于其父级的宽度百分比。 |
MainViewScaleFactor |
设置打开侧边菜单(左侧或右侧)时 MainView 将缩放到的缩放百分比。 |
Position |
指定位置是右还是左还是主视图,如果没有指定,它将被视为主视图。 |
MenuGestureEnabled |
启用/禁用侧滑手势选项以打开/关闭菜单/视图。 |
打开/关闭侧面菜单视图(弹出)
从代码
通过滑动手势或通过设置您在 xaml 中定义的属性State
来打开左右菜单/视图:SideMenuView
SideMenuView.State = SideMenuState.MainViewShown; //show main view and close both left and right menu view
SideMenuView.State = SideMenuState.RightMenuShown; //Open right menu view
SideMenuView.State = SideMenuState.LeftMenuShown; //Open left menu view
来自 xml
默认情况下,左右菜单是关闭的(State
属性的默认值为MainViewShown
),如果您希望最初打开一个菜单,请在您的 xaml 中更改它的值:
<xct:SideMenuView x:Name="SideMenuView" State="LeftMenuShown">
一种混合方法是绑定State
到一个属性并根据您的逻辑在您的代码中更改它。
笔记
目前它仅支持 iOS 和 Android(不支持 uwp)。