Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling back button presses #47

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,26 @@ Several parameter keys have been pre-defined and are using by the `Burkus.Mvvm.M

The `NavigationParameters` object exposes some handy properties `.UseAnimatedNavigation` and `.UseModalNavigation` so you can easily set or check the value of these properties.

### Handling back button presses
By default, back button presses on Android/Windows will bypass `Burkus.Mvvm.Maui` which would mean lifecycle events and parameter passing may not happen when you expect them to. You can allow `Burkus.Mvvm.Maui` to handle the back button navigation for a page by turning it into a `Burkus...Page`. For example, below a `ContentPage` is turned into a `BurkusContentPage`.

``` xml
<burkus:BurkusContentPage
...
xmlns:burkus="http://burkus.co.uk"
...>
```

``` csharp
public partial class HomePage : BurkusContentPage
```

The page types available are:
- `BurkusContentPage`
- `BurkusNavigationPage`
- `BurkusTabbedPage`
- `BurkusFlyoutPage`

## Dialog service
`IDialogService` is automatically registered by `.UseBurkusMvvm(...)`. It is a testable service that is an abstraction over [the MAUI alerts/pop-ups/prompts/action sheets](https://learn.microsoft.com/en-us/dotnet/maui/user-interface/pop-ups).

Expand Down
5 changes: 3 additions & 2 deletions samples/DemoApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using DemoApp.ViewModels;
using DemoApp.Views;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;

namespace DemoApp;

Expand All @@ -23,11 +24,11 @@ public static MauiApp CreateMauiApp()
if (preferences.ContainsKey(PreferenceKeys.Username))
{
// we are logged in to the app
await navigationService.Push<HomePage>();
await navigationService.Navigate("/HomePage");
}
else
{
await navigationService.Push<LoginPage>();
await navigationService.Navigate("/LoginPage");
}
});
})
Expand Down
5 changes: 5 additions & 0 deletions samples/DemoApp/ViewModels/RegisterViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public RegisterViewModel(

#endregion Constructors

public override async Task OnNavigatingFrom(NavigationParameters parameters)
{
await base.OnNavigatingFrom(parameters);
}

#region Commands

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/ChangeUsernamePage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.ChangeUsernamePage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -37,4 +37,4 @@
Text="{x:Static properties:Resources.Button_Finish}" />
</Grid>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/ChangeUsernamePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class ChangeUsernamePage : ContentPage
public partial class ChangeUsernamePage : BurkusContentPage
{
public ChangeUsernamePage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Flyouts/ContactsPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.ContactsPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -39,4 +39,4 @@
Text="{x:Static properties:Resources.Home_Button_TabbedPageDemo}" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Flyouts/ContactsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class ContactsPage : ContentPage
public partial class ContactsPage : BurkusContentPage
{
public ContactsPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Flyouts/DemoFlyoutPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<FlyoutPage
<burkus:BurkusFlyoutPage
x:Class="DemoApp.Views.DemoFlyoutPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand All @@ -21,4 +21,4 @@
</x:Arguments>
</NavigationPage>
</FlyoutPage.Detail>
</FlyoutPage>
</burkus:BurkusFlyoutPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Flyouts/DemoFlyoutPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class DemoFlyoutPage : FlyoutPage
public partial class DemoFlyoutPage : BurkusFlyoutPage
{
public DemoFlyoutPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Flyouts/RemindersPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.RemindersPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -28,4 +28,4 @@
</VerticalStackLayout>
</ScrollView>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Flyouts/RemindersPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class RemindersPage : ContentPage
public partial class RemindersPage : BurkusContentPage
{
public RemindersPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Flyouts/TodoPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.TodoPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand All @@ -26,4 +26,4 @@
Text="{x:Static properties:Resources.Todo_Button_PushNewPageIntoDetail}" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Flyouts/TodoPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class TodoPage : ContentPage
public partial class TodoPage : BurkusContentPage
{
public TodoPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/HomePage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.HomePage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -67,4 +67,4 @@
Text="{x:Static properties:Resources.Home_Button_FlyoutPageDemo}" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/HomePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class HomePage : ContentPage
public partial class HomePage : BurkusContentPage
{
public HomePage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/LoginPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.LoginPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -54,4 +54,4 @@
</Label>
</Grid>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/LoginPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class LoginPage : ContentPage
public partial class LoginPage : BurkusContentPage
{
public LoginPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/RegisterPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.RegisterPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -30,4 +30,4 @@
Text="{x:Static properties:Resources.Button_GoBack}" />
</Grid>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/RegisterPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class RegisterPage : ContentPage
public partial class RegisterPage : BurkusContentPage
{
public RegisterPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Tabs/AlphaTabPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.AlphaTabPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -35,4 +35,4 @@
Text="{x:Static properties:Resources.Button_GoBack}" />
</Grid>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Tabs/AlphaTabPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class AlphaTabPage : ContentPage
public partial class AlphaTabPage : BurkusContentPage
{
public AlphaTabPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Tabs/BetaTabPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.BetaTabPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -35,4 +35,4 @@
Text="{x:Static properties:Resources.Button_GoBack}" />
</Grid>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Tabs/BetaTabPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class BetaTabPage : ContentPage
public partial class BetaTabPage : BurkusContentPage
{
public BetaTabPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Tabs/CharlieTabPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.CharlieTabPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -35,4 +35,4 @@
Text="{x:Static properties:Resources.Button_GoBack}" />
</Grid>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Tabs/CharlieTabPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class CharlieTabPage : ContentPage
public partial class CharlieTabPage : BurkusContentPage
{
public CharlieTabPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/Tabs/DemoTabsPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage
<burkus:BurkusTabbedPage
x:Class="DemoApp.Views.DemoTabsPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand All @@ -13,4 +13,4 @@
<views:AlphaTabPage Title="Alpha" />
<views:BetaTabPage Title="Beta" />
<views:CharlieTabPage Title="Charlie" />
</TabbedPage>
</burkus:BurkusTabbedPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/Tabs/DemoTabsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class DemoTabsPage : TabbedPage
public partial class DemoTabsPage : BurkusTabbedPage
{
public DemoTabsPage()
{
Expand Down
4 changes: 2 additions & 2 deletions samples/DemoApp/Views/UriTestPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
<burkus:BurkusContentPage
x:Class="DemoApp.Views.UriTestPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand Down Expand Up @@ -48,4 +48,4 @@
Text="{x:Static properties:Resources.UriTest_Button_SwitchToChangeUsername}" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
</burkus:BurkusContentPage>
2 changes: 1 addition & 1 deletion samples/DemoApp/Views/UriTestPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DemoApp.Views;

public partial class UriTestPage : ContentPage
public partial class UriTestPage : BurkusContentPage
{
public UriTestPage()
{
Expand Down
14 changes: 14 additions & 0 deletions src/Burkus.Mvvm.Maui/Models/Pages/BackButtonNavigator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Burkus.Mvvm.Maui;

internal static class BackButtonNavigator
{
internal static bool HandleBackButtonPressed()
{
var navigationService = ServiceResolver.Resolve<INavigationService>();

_ = navigationService.Pop();

// On Android and Windows, prevent the default back button behaviour
return true;
}
}
9 changes: 9 additions & 0 deletions src/Burkus.Mvvm.Maui/Models/Pages/BurkusContentPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Burkus.Mvvm.Maui;

public class BurkusContentPage : ContentPage
{
protected override bool OnBackButtonPressed()
{
return BackButtonNavigator.HandleBackButtonPressed();
}
}
9 changes: 9 additions & 0 deletions src/Burkus.Mvvm.Maui/Models/Pages/BurkusFlyoutPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Burkus.Mvvm.Maui;

public class BurkusFlyoutPage : FlyoutPage
{
protected override bool OnBackButtonPressed()
{
return BackButtonNavigator.HandleBackButtonPressed();
}
}
Loading
Loading