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