Reputation: 139
I'm working with C# xamarin forms android and I'm trying to close my sidebar when the user touches de backbuttom but I'm not able to do it. Instead, my aplications closeses.
HomePage.xaml.cs code:
public partial class HomePage : ContentPage
{
string[] subs;
public HomePage()
{
//NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
my sidebar AppShell.xaml.cs code:
public partial class AppShell : Xamarin.Forms.Shell
{
public AppShell()
{
InitializeComponent();
nameuser.Title = Preferences.Get("displayName", "Default");
versionadoAPP.Text = "v" + $"{VersionTracking.CurrentBuild}.{VersionTracking.CurrentVersion}";
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
Image of my app:
How could I resolve that? Thank tou very much!
Upvotes: 0
Views: 106
Reputation: 514
If you have a home page you want to take the user when the user touches the Android back button, you can first check whether the sideMenu is open and if so close it with the back button and then with next touch send the user to the desired main page as below;
protected override bool OnBackButtonPressed()
{
if (Shell.Current.FlyoutIsPresented)
{
Shell.Current.FlyoutIsPresented = false;
}
else
{
Device.BeginInvokeOnMainThread(async () =>
{
await Shell.Current.GoToAsync($"//{nameof(<your home page>)}");
});
}
return true;
}
Upvotes: 0
Reputation: 16572
The easiest way that do this is to set the flyout presented property.
Try something like this:
Shell.Current.FlyoutIsPresented = false;
Good luck!
Upvotes: 2