您可以使用<Shell.FlyoutHeaderTemplate> 添加自定义菜单项,然后使用绑定隐藏/显示菜单。
步骤 1. 实现 InverseBoolConverter 类
namespace MyMobileApp.Converters
{
public class InverseBoolConverter : Xamarin.Forms.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value;
}
}
}
第 2 步:在 AppShell.xaml 类中添加 InverseBoolConverter 引用。然后在顶部添加 xmlns:converter="clr-namespace:MyMobileApp.Converters"(在 Shell 标记内)。然后在<ResourceDictionary> 标签之后添加<converter:InverseBoolConverter x:Key="cvInverse"></converter:InverseBoolConverter>。
第 3 步:在 AppShell.xaml 中的 </FlyoutItem> 标记之后添加以下代码。
<Shell.FlyoutHeaderTemplate>
<DataTemplate>
<StackLayout BackgroundColor="White">
<!--Header-->
<StackLayout Orientation="Horizontal" BackgroundColor="LightGray" Padding="20" IsVisible="{Binding IsUserLogedin}">
<Label Text="Watchlist" TextColor="Black" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand" VerticalTextAlignment="Center">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding WatchlistCommand}"/>
</Label.GestureRecognizers>
</Label>
<Label Text="Logout" TextColor="Black" VerticalOptions="CenterAndExpand" HorizontalOptions="End" VerticalTextAlignment="Center">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding LogoutCommand}"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
<StackLayout Orientation="Horizontal" BackgroundColor="LightGray" Padding="20" IsVisible="{Binding IsUserLogedin, Converter={StaticResource cvInverse}}">
<Label Text="Register" TextColor="Black" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand" VerticalTextAlignment="Center">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding RegisterCommand}"/>
</Label.GestureRecognizers>
</Label>
<Label Text="Login" TextColor="Black" VerticalOptions="CenterAndExpand" HorizontalOptions="End" VerticalTextAlignment="Center">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding LoginCommand}"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
</StackLayout>
</DataTemplate>
</Shell.FlyoutHeaderTemplate>
第 4 步:在 AppShell.xaml.cs 类中实现绑定
public Xamarin.Forms.Command LoginCommand { get; }
public Xamarin.Forms.Command RegisterCommand { get; }
public Xamarin.Forms.Command LogoutCommand { get; }
public Xamarin.Forms.Command WatchlistCommand { get; }
private bool _isUserLoggedIn;
public bool IsUserLogedin
{
get => _isUserLoggedIn;
set => SetProperty(ref _isUserLoggedIn, value);
}
public AppShell()
{
InitializeComponent();
LoginCommand = new Xamarin.Forms.Command(MenuClicked);
RegisterCommand = new Xamarin.Forms.Command(MenuClicked);
LogoutCommand = new Xamarin.Forms.Command(MenuClicked);
WatchlistCommand = new Xamarin.Forms.Command(MenuClicked);
BindingContext = this;
}
private async void MenuClicked(object obj)
{
}
protected bool SetProperty<T>(ref T backingStore, T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "", System.Action onChanged = null)
{
if (System.Collections.Generic.EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}