【问题标题】:Xamarin multiple flyouts in one pageXamarin 在一页中有多个弹出窗口
【发布时间】:2021-06-22 17:57:01
【问题描述】:

我一直在研究 Xamarin.Forms Shell。默认情况下,Shell 中可用的浮出控件允许从屏幕左侧滑动或点击工具栏菜单按钮以使浮出控件显示在屏幕左侧。

我还看到了这种情况的一种变体,其中工具栏和浮出控件被翻转,因此工具栏按钮和浮出控件位于屏幕右侧。

我的问题是 - 我想在屏幕上显示多个弹出窗口。 Shell 有效,但如果我使用 Shell(左侧或右侧,但不能同时使用两者),我似乎只能有一个弹出按钮。有没有办法在 Shell 中使用多个弹出窗口?

或者,有没有更好的解决方案?

【问题讨论】:

    标签: xaml xamarin xamarin.forms flyout xamarin.forms.shell


    【解决方案1】:

    您可以使用Xamarin.CommunityToolkit package 中的SideMenuViewPage,它已经可用但尚未记录。

    您可以在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>
    

    可用属性

    Property Description
    MenuWidthPercentage Set the width percentage of your menu relative to it parent.
    MainViewScaleFactor Set the scale percentage which MainView will be scaled to when a side menu (left or right) is opened.
    Position Specifies position whether it is right or left or main, if not specified it will be considered as the MainView.
    MenuGestureEnabled Enable/Disable side swipe gesture option to open/close menu/view.

    打开/关闭侧边菜单视图(弹出)

    来自代码

    通过滑动手势或设置您在 xaml 中定义的 SideMenuView 的属性 State 可以打开左右菜单/视图:

    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
    

    来自 xaml

    默认情况下左右菜单是关闭的(State 属性的默认值为MainViewShown),如果您希望最初打开一个菜单,请在您的 xaml 中更改它的值:

    <xct:SideMenuView x:Name="SideMenuView" State="LeftMenuShown">
    

    一种混合方法是将State 绑定到一个属性,并根据您的逻辑在您的代码中更改它。

    注意

    目前仅支持 iOS 和 Android(不支持 uwp)。

    【讨论】:

    • 哇,太好了,我会试试这个。这不是我以前见过的。感谢您的详细回复。
    • 不客气,如果您在实施过程中发现问题,请告诉我,或者如果您将来有可能改进它的想法,您可以在 GitHub: XamarinCommunityToolkit 上打开问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    相关资源
    最近更新 更多