{"id":21730109,"url":"https://github.com/nventive/chinook.backbuttonmanager","last_synced_at":"2025-08-17T07:05:08.962Z","repository":{"id":40244287,"uuid":"269188299","full_name":"nventive/Chinook.BackButtonManager","owner":"nventive","description":"Handle and possibly intercept back navigation on Windows, iOS, Android and WebAssembly","archived":false,"fork":false,"pushed_at":"2025-05-20T13:45:13.000Z","size":90,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-08-01T01:13:12.100Z","etag":null,"topics":["chinook","dotnet","mobile","mvvm","uno-platform","xamarin"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nventive.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-06-03T20:31:02.000Z","updated_at":"2025-06-20T05:39:10.000Z","dependencies_parsed_at":"2023-01-21T04:54:01.228Z","dependency_job_id":"12d6a1b8-3727-4744-89fd-e8fb3831dde4","html_url":"https://github.com/nventive/Chinook.BackButtonManager","commit_stats":{"total_commits":39,"total_committers":14,"mean_commits":"2.7857142857142856","dds":0.7692307692307692,"last_synced_commit":"a438f2d0b05c2b14352997474577f15c024b0a9b"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":"nventive/Template","purl":"pkg:github/nventive/Chinook.BackButtonManager","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nventive%2FChinook.BackButtonManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nventive%2FChinook.BackButtonManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nventive%2FChinook.BackButtonManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nventive%2FChinook.BackButtonManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nventive","download_url":"https://codeload.github.com/nventive/Chinook.BackButtonManager/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nventive%2FChinook.BackButtonManager/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270816699,"owners_count":24650858,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chinook","dotnet","mobile","mvvm","uno-platform","xamarin"],"created_at":"2024-11-26T04:12:28.490Z","updated_at":"2025-08-17T07:05:08.912Z","avatar_url":"https://github.com/nventive.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿# Chinook.BackButtonManager \n\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](LICENSE) ![Version](https://img.shields.io/nuget/v/Chinook.BackButtonManager.Abstractions?style=flat-square) ![Downloads](https://img.shields.io/nuget/dt/Chinook.BackButtonManager.Abstractions?style=flat-square)\n\nThe `Chinook.BackButtonManager` packages provide recipes to ease the handling of back buttons in .Net applications. It was designed for MVVM applications, but should work with other patterns too.\n\n## Cornerstones\n\n- **Highly Extensible**\n  - Everything is interface-based to easily allow more implementations.\n  - A single framework can't cover everything. Our architecture is designed in a way that allows you to extend this foundation to support more use-cases.\n- **Simple**\n  - The recipes from these packages are ultra-simple but are still complete enough to support edge cases.\n\n### More like this\nThe Chinook namespace has other recipes for .Net MVVM applications.\n- [Chinook.DynamicMvvm](https://github.com/nventive/Chinook.DynamicMvvm): MVVM libraries for extensible and declarative ViewModels.\n- [Chinook.Navigation](https://github.com/nventive/Chinook.Navigation): Navigators for ViewModel-first navigation.\n- [Chinook.DataLoader](https://github.com/nventive/Chinook.DataLoader): Customizable async data loading recipes.\n\n## Getting Started\n\n### Uno projects\n\nBackButtonManager is especially well-integrated with [Uno](https://platform.uno/). Here is how to use it in a project which includes the Uno platform:\n\n1. Add a package reference to `Chinook.BackButtonManager.Uno.WinUI` (for WinUI or MAUI apps).\n1. Create a single instance of a `BackButtonManager` which you will use throughout your project.\n   ```csharp\n   var manager = new BackButtonManager();\n   ```\n\n1. In your app's Startup, add the source that the manager uses to detect back button presses:\n   ```csharp\n   // This must be executed on the dispatcher\n   var source = new SystemNavigationBackButtonSource();\n   manager.AddSource(source);\n   ```\n\n   \u003e ❗ Note that `SystemNavigationManager` no longer works on Windows applications that are not UWP. On WinUI, you can no longer have a global back button in the title bar of the application.\n\n1. Add handlers for each action you want to take when the back button is pressed:\n   ```csharp\n   manager.AddHandler(new BackButtonHandler(\n\t   name: \"TODO handler name\",\n   \tcanHandle: () =\u003e CanYourMethodBeCalled(),\n   \thandle: async ct =\u003e await YourMethod(ct)\n   ));\n   ```\n\n### Other projects\n\nIf your project does not use Uno, you can certainly use BackButtonManager! Here's how:\n\n1. Add a package reference to `Chinook.BackButtonManager`.\n1. Create a single instance of a `BackButtonManager` which you will use throughout your project.\n\n   ```csharp\n   var manager = new BackButtonManager();\n   ```\n\n1. You will need to create a source which implements the `IBackButtonSource` interface. In your app's Startup, add this source so that BackButtonManager can use it to detect back button presses. \n\n   ```csharp\n   var source = new MyBackButtonSource();\n   manager.AddSource(source);\n   ```\n\n1. Add handlers for each action you want to take when the back button is pressed:\n\n   ```csharp\n   manager.AddHandler(new BackButtonHandler(\n     name: \"TODO handler name\",\n     canHandle: () =\u003e CanYourMethodBeCalled(),\n     handle: async ct =\u003e await YourMethod(ct)\n   ));\n   ```\n\n## Features\n### Create back button sources\nUsing `IBackButtonSource`, you can implement a back button source. You can see that as an abstraction of a button. You could create sources for things like the following.\n- The hardware back button on Android.\n- The escape key from your keyboard.\n- The back button from your mouse.\n\nOnce you have a back button source, simply add it to a `IBackButtonManager` using `AddSource`.\n\n#### Create a back button source for XButton1\nThis is a source for the XButton1, which is the back button on a mouse. This is useful for desktop applications.\n\n```csharp\nMicrosoft.UI.Xaml.Window window;\n// (Make sure you initialize the window and its content.)\nvar source = new XButton1BackButtonSource(window.Content);\n```\n\n#### Create a custom back button source\nThe interface `IBackButtonSource` is very simple. You can implement your own sources easily.\n\n### Create back button handlers\nUsing `IBackButtonHandler`, you can create the objects that react to back requests. Handlers can be added to (or removed from) a `IBackButtonManager` at any point.\n\n#### Create global handlers\nThat's one of the main use-case of this recipe. You likely want to create a default action to perform when a back is requested.\n\nHere is some code showing how to setup a `IBackButtonManager` with a default handler in a context using Microsoft.Extensions.DependencyInjection and [Chinook.Navigation](https://github.com/nventive/Chinook.Navigation).\n```csharp\npublic static IServiceCollection AddDefaultBackHandler(this IServiceCollection services)\n{\n  return services\n    .AddSingleton\u003cIBackButtonManager\u003e(s =\u003e\n    {\n      var manager = new BackButtonManager();\n\n      var sectionsNavigator = s.GetRequiredService\u003cISectionsNavigator\u003e();\n      manager.AddHandler(new BackButtonHandler(\n        name: \"DefaultSectionsNavigatorHandler\",\n        canHandle: () =\u003e sectionsNavigator.CanNavigateBackOrCloseModal(),\n        handle: async ct =\u003e await sectionsNavigator.NavigateBackOrCloseModal(ct)));\n\n      return manager;\n    });\n}\n```\n\n#### Create temporary handlers\nThis can be useful when your page has advances states or secondary views. Imagine having a drawer or side menu. When that secondary view is active, it is likely that you want your back button to dismiss that secondary view rather than navigating to the previous page.\n\n```csharp\npublic MainPageViewModel(IBackButtonManager manager)\n{\n\tvar customHandler = new BackButtonHandler(\"CustomBackHandler\",\n\t\t// The handler will only be invoked when the side panel is open.\n\t\tcanHandle: () =\u003e IsSidePanelOpen,\n\t\t\n\t\t// Close the side panel when a back is requested.\n\t\thandle: async ct =\u003e IsSidePanelOpen = false\n\t);\n\tvar subscription = manager.RegisterHandler(customHandler);\n\n\t// Automatically remove the handler when this page gets disposed.\n\tthis.AddDisposable(subscription);\n}\n\npublic bool IsSidePanelOpen\n{\n\tget =\u003e this.Get\u003cbool\u003e(initialValue: false);\n\tset =\u003e this.Set(value);\n}\n```\n\u003e 💡 This sample shows a ViewModel written using [Chinook.DynamicMvvm](https://github.com/nventive/Chinook.DynamicMvvm).\n\n#### Specify an handler's priority\nIt is possible to specify a priority when calling `IBackButtonManager.AddHandler`. The highest priority handlers will be evaluated first.\n\n## Legacy\n\n#### Create a source from `SystemNavigationManager`\nThis can be useful for UWP or Uno.UI applications. The source is based on the `SystemNavigationManager.BackRequested` event.\n```csharp\n// This must be executed on the dispatcher\nvar source = new SystemNavigationBackButtonSource();\n```\n\n## Breaking Changes\n\nPlease consult [BREAKING_CHANGES.md](BREAKING_CHANGES.md) for more information about migration.\n\n## License\n\nThis project is licensed under the Apache 2.0 license - see the\n[LICENSE](LICENSE) file for details.\n\n## Contributing\n\nPlease read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the process for\ncontributing to this project.\n\nBe mindful of our [Code of Conduct](CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnventive%2Fchinook.backbuttonmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnventive%2Fchinook.backbuttonmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnventive%2Fchinook.backbuttonmanager/lists"}