微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Xamarin shell 中创建嵌套导航

如何解决在 Xamarin shell 中创建嵌套导航

我正在尝试使用 Xamarin Forms 开发 Android 应用程序。 我想要嵌套导航

Header (on tap dropdown items show)
   Item1
   Item2

show flyout (shell) with submenu 开始,我为此使用了大部分代码,但它不起作用

这是我添加认应用程序的代码

FlyoutItemTemplateSelector.cs

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace MauiApp.Shared
{
    public class FlyoutItemTemplateSelector : DataTemplateSelector
    {
        public DataTemplate NavigationHeaderTemplate { get; set; }
        public DataTemplate NavigationItemTemplate { get; set; }

        private List<string> list = new List<string> { "Header1","Header2"};

        //There should be more than one header in shell


        protected override DataTemplate OnSelectTemplate(object item,BindableObject container)
        {
            if (item is ShellGroupItem && list.Contains(((ShellGroupItem)item).Title))
            {
                // Make sure a header item is not clickable.
                ((ShellGroupItem)item).IsEnabled = false;
                return NavigationHeaderTemplate;
            }
            else
                return NavigationItemTemplate;
        }
    }
}

AppShell.xaml

    <DataTemplate x:Key="FlyoutItemTemplate">
        <Grid>
            <Grid.ColumnDeFinitions>
                <ColumnDeFinition Width="0.25*" />
                <ColumnDeFinition Width="0.75*" />
            </Grid.ColumnDeFinitions>
            <Label Grid.Column="1"
               Text="{Binding Title}"
               TextColor="White"
               VerticalTextAlignment="Center" 
               BackgroundColor="Aqua"/>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="FlyoutHeaderTemplate">
        <StackLayout Orientation="Vertical">
            <Label HeightRequest="35"
               Margin="20,0"
               Text="{Binding Title}"
               TextColor="Black"
               VerticalTextAlignment="Center" >
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
                </Label.GestureRecognizers>

            </Label>
        </StackLayout>
    </DataTemplate>
    <controls:FlyoutItemTemplateSelector
    x:Key="FlyoutTemplateSelector"
    NavigationHeaderTemplate="{StaticResource FlyoutHeaderTemplate}"
    NavigationItemTemplate="{StaticResource FlyoutItemTemplate}" />

    </ResourceDictionary>
</Shell.Resources>


<FlyoutItem Title="Example"  Shell.TabBarIsVisible="False" FlyoutdisplayOptions="AsMultipleItems" x:Name="example" Style="{StaticResource BaseStyle}">
    <ShellContent Title="Header1"  ContentTemplate="{DataTemplate local:AboutPage}"/>
    <ShellContent Title="Item1" IsVisible="{Binding Changed}" ContentTemplate="{DataTemplate local:AboutPage}"/>
    <ShellContent Title="Item2" IsVisible="{Binding Changed}" ContentTemplate="{DataTemplate local:AboutPage}"/>
</FlyoutItem>

和回溯代码

using MauiApp.viewmodels;
using MauiApp.Views;
using System;
using System.Collections.Generic;
using Xamarin.Forms;

using MauiApp.Shared;
using System.ComponentModel;

namespace MauiApp
{
    public partial class AppShell : Xamarin.Forms.Shell
    {
        Status status;
        public AppShell()
        {
            InitializeComponent();
            Routing.RegisterRoute(nameof(ItemDetailPage),typeof(ItemDetailPage));
            Routing.RegisterRoute(nameof(NewItemPage),typeof(NewItemPage));
            status = new Status();
            this.BindingContext = status;
        }
        private async void OnMenuItemClicked(object sender,EventArgs e)
        {
            await Shell.Current.GoToAsync("//LoginPage");
        }
        private void TapGestureRecognizer_Tapped(object sender,EventArgs e)
        {
            status.Changed = !status.Changed;
        }
    }
    public class Status : INotifyPropertyChanged
    {
        bool _changed = false;
        public bool Changed
        {
            get
            {
                return _changed;
            }
            set
            {
                if (_changed != value)
                {
                    _changed = value;
                    OnPropertyChanged("Changed");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
        }
    }
}

我现在得到的只是导航,将 Header1 设置为 NavigationItemTemplate 而不是 NavigationHeaderTemplate。

我的问题是:

  1. 这是获得“嵌套”导航的好方法吗(如果不是更好的方法
  2. 为什么 Header 被识别为 Item 以及如何修复它?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?