You can use <Shell.FlyoutHeaderTemplate> to add custom menu items and then use the binding to hide/show menus.
Step 1. Implement the InverseBoolConverter class
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;
}
}
}
Step 2: Add InverseBoolConverter reference in AppShell.xaml class.Then add xmlns:converter="clr-namespace:MyMobileApp.Converters" on top (within Shell Tag). and then add <converter:InverseBoolConverter x:Key="cvInverse"></converter:InverseBoolConverter> just after <ResourceDictionary> tag.
Step 3: Add below code in AppShell.xaml just after </FlyoutItem> tag.
<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>
Step 4: Implement bindings in AppShell.xaml.cs class
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;
}