Net MAUI Shell 菜单,侧边栏内可折叠。

huangapple go评论168阅读模式
英文:

Net MAUI Shell Menu with collapse inside sidebar

问题

你好,我想知道是否可以在侧边栏中实现带有选项的可折叠菜单项。

就像这样:
Net MAUI Shell 菜单,侧边栏内可折叠。

我尝试使用menuflyoutitem,但它只适用于选项卡式菜单,我需要一个像图片中那样的菜单。

英文:

Hi i was wondering if it's possible to implement a collapsable menuitem with options inside the side bar.

like this:
Net MAUI Shell 菜单,侧边栏内可折叠。

I've tried using menuflyoutitem but it is only for tabbed menu I need a menu just like the one in the picture

答案1

得分: 1

为此,您可以创建一个动画Flyout菜单来显示隐藏在主内容后面的菜单。

有关更多信息,请参考Andrew的文章《Fancy Flyout菜单》1。虽然文章是基于Xamarin的,但在MAUI中实现基本相同。

Fancy菜单利用ContentPageContentView的组合来创建Flyout控件,而不是使用内置控件。我们的控件将分为3部分:

  • 容器 - Fancy菜单的入口点
  • 菜单 - 隐藏的菜单
  • 主内容 - 最初呈现的页面

在这里,我们只需将要添加到菜单页面的视图添加到菜单页面即可。

菜单页面是一个标准的ContentView,用于显示隐藏在主内容后面的菜单。由于容器完成了所有繁重的工作,所以您只需要在此文件中实现菜单即可。它将被正确样式化以占用正确的宽度,而不会溢出到主内容上。

您可以参考以下代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             BackgroundColor="SkyBlue"
             x:Class="FancyMenu.MenuPage">

    <ContentView.Resources>
        <ResourceDictionary>
            <Style TargetType="Label">
                <Setter Property="HorizontalTextAlignment" Value="Center" />
            </Style>
        </ResourceDictionary>
    </ContentView.Resources>
    
    <Grid>
        <!-- 省略部分内容 -->
    </Grid>
</ContentView>

结果如下:

Net MAUI Shell 菜单,侧边栏内可折叠。

英文:

For this, you can create an animating Flyout Menu to display the menu that hides behind the Main Content.

For more information, you can refer to article Fancy Flyout Menu from Andrew. Although the article is based on Xamarin, the implementation is basically the same in MAUI.

The Fancy Menu utilizes a combination of ContentPage's and ContentView's to create the Flyout control instead of using the built in controls. Our control will be broken into 3 parts

  • Container - Entry point of the Fancy Menu
  • Menu - The hidden menu
  • MainContent - The page that is initially rendered

Here,we just need to add the views we want to the Menu page.

The Menu page is a standard ContentView that displays the menu that hides behind the Main Content. Since the Container does all the heavy lifting all you need to do in this file is implement your menu. It will be styled correctly to take up the correct width and not overflow onto the Main Content.

You can refer to the following code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; 
&lt;ContentView xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             xmlns:d=&quot;http://xamarin.com/schemas/2014/forms/design&quot;
             xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
             mc:Ignorable=&quot;d&quot;
             BackgroundColor=&quot;SkyBlue&quot;
             x:Class=&quot;FancyMenu.MenuPage&quot;&gt;

    &lt;ContentView.Resources&gt;
        &lt;ResourceDictionary&gt;
            &lt;Style TargetType=&quot;Label&quot;&gt;
                &lt;Setter Property=&quot;HorizontalTextAlignment&quot; Value=&quot;Center&quot; /&gt;
            &lt;/Style&gt;
        &lt;/ResourceDictionary&gt;
    &lt;/ContentView.Resources&gt;
    
    &lt;Grid&gt;
        &lt;Grid.RowDefinitions&gt;
            &lt;RowDefinition Height=&quot;Auto&quot; /&gt;
            &lt;RowDefinition Height=&quot;Auto&quot; /&gt;
            &lt;RowDefinition Height=&quot;*&quot; /&gt;
        &lt;/Grid.RowDefinitions&gt;
        
        &lt;StackLayout Grid.Row=&quot;0&quot; Padding=&quot;{OnPlatform Android=&#39;50, 25&#39;, iOS=&#39;50, 50, 50, 25&#39;}&quot;&gt;
            &lt;Label Text=&quot;Fancy Menu&quot;
                   TextColor=&quot;White&quot;
                   FontSize=&quot;30&quot;
                   HorizontalOptions=&quot;CenterAndExpand&quot;/&gt;
            &lt;BoxView BackgroundColor=&quot;White&quot; HeightRequest=&quot;1&quot; HorizontalOptions=&quot;FillAndExpand&quot; /&gt;
        &lt;/StackLayout&gt;

        &lt;StackLayout Grid.Row=&quot;1&quot; Padding=&quot;10, 0, 10, 25&quot;&gt;
            &lt;Label Text=&quot;This is the fancy menu where youc an customize your control just like any page. I prefer to place header or profile content above the buttons, which is what this space is designed for.&quot; /&gt;
        &lt;/StackLayout&gt;

        &lt;Grid Grid.Row=&quot;2&quot; Padding=&quot;25, 0, 25, 25&quot; VerticalOptions=&quot;FillAndExpand&quot;&gt;
            &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition Height=&quot;6*&quot; /&gt;
                &lt;RowDefinition Height=&quot;*&quot; /&gt;
            &lt;/Grid.RowDefinitions&gt;
            &lt;StackLayout Grid.Row=&quot;0&quot; Spacing=&quot;20&quot;
                         VerticalOptions=&quot;CenterAndExpand&quot;&gt;
                &lt;StackLayout.Resources&gt;
                    &lt;ResourceDictionary&gt;
                        &lt;Style TargetType=&quot;Frame&quot;&gt;
                            &lt;Setter Property=&quot;BackgroundColor&quot; Value=&quot;Lavender&quot; /&gt;
                            &lt;Setter Property=&quot;CornerRadius&quot; Value=&quot;10&quot; /&gt;
                            &lt;Setter Property=&quot;Padding&quot; Value=&quot;15, 10&quot; /&gt;
                        &lt;/Style&gt;
                    &lt;/ResourceDictionary&gt;
                &lt;/StackLayout.Resources&gt;
                &lt;Frame&gt;
                    &lt;Label Text=&quot;Home&quot; /&gt;
                &lt;/Frame&gt;
                &lt;Frame&gt;
                    &lt;!--&lt;Label Text=&quot;Settings&quot; /&gt;--&gt;
                    &lt;Picker x:Name=&quot;picker&quot;
                       Title=&quot;DropDown Menu&quot;&gt;
                        &lt;Picker.ItemsSource&gt;
                            &lt;x:Array Type=&quot;{x:Type x:String}&quot;&gt;
                                &lt;x:String&gt;jQuery&lt;/x:String&gt;
                                &lt;x:String&gt;Script&lt;/x:String&gt;
                                &lt;x:String&gt;Net&lt;/x:String&gt;
                            &lt;/x:Array&gt;
                        &lt;/Picker.ItemsSource&gt;
                    &lt;/Picker&gt;
                &lt;/Frame&gt;
                &lt;Frame&gt;
                    &lt;Label Text=&quot;Contact&quot; /&gt;
                &lt;/Frame&gt;
                &lt;Frame&gt;
                    &lt;Label Text=&quot;About&quot; /&gt;
                &lt;/Frame&gt;
            &lt;/StackLayout&gt;

            &lt;StackLayout Grid.Row=&quot;1&quot; VerticalOptions=&quot;EndAndExpand&quot;&gt;
                &lt;Label Text=&quot;Hi&quot; /&gt;
                &lt;Label Text=&quot;Open Source Software&quot; /&gt;
            &lt;/StackLayout&gt;
        &lt;/Grid&gt;
        
    &lt;/Grid&gt;
    
&lt;/ContentView&gt;

The result is:

Net MAUI Shell 菜单,侧边栏内可折叠。

huangapple
  • 本文由 发表于 2023年6月15日 00:01:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定