> For the complete documentation index, see [llms.txt](https://adospace.gitbook.io/mauireactor/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://adospace.gitbook.io/mauireactor/components/navigation/back-button.md).

# Back button

The back button in MAUI has different behavior and settings based on whether you're hosting the child page under a NavigationPage or Shell.

## Shell

Under the Shell, you can use the BackButtonBehavior class to customize the BackButton.

To handle back button behavior you have to provide a call-back action in the `OnBackButtonPressed`

```csharp
new ContentPage()
    .OnBackButtonPressed(()=>...);
```

You can also set visibility, icon source etc directly to the Page, like this:

```csharp
new ContentPage()
    .BackButtonIsVisible(true/false)
    .BackButtonIsEnabled(true/false)
    .BackButtonText(...)
    .BackButtonIcon(...)
```

## NavigationPage

If the child page is hosted under a NavigationPage the BackButton can be disabled using the AttachedProperty HasBackButtonDisable dependency property.\
In MauiReactor use this code:

For the BackButton pressed event you have instead to hack it a bit as there isn't a BackButtonPressed event out of the box and the OnBackButtonPressed can't work.<br>

To handle such an event we have to create a Page-derived custom class and override the OnBackButtonPressed method:

```csharp
public class CustomContentPage : MauiControls.ContentPage
{
    public event EventHandler BackButtonPressed;

    protected override bool OnBackButtonPressed()
    {
        BackButtonPressed?.Invoke(this, EventArgs.Empty);
        return base.OnBackButtonPressed();
    }
}
```

{% hint style="info" %}
Put the above code in a separate file/namespace different from the namespace containing the component that will scaffold it.
{% endhint %}

Finally, scaffold and use it in your component:

```csharp
[Scaffold(typeof(Project24.Controls.CustomContentPage))]
public partial class CustomContentPage
{
}

public class MainPage : Component
{
    public override VisualNode Render()
    {
        return new NavigationPage()
        {
            new ContentPage()
            {
                new Button("Move To Page")
                    .VCenter()
                    .HCenter()
                    .OnClicked(OpenChildPage)
            }
            .Title("Main Page")
        }
            ;
    }

    private async void OpenChildPage()
    {
        await Navigation.PushAsync<ChildPage>();
    }
}

public class ChildPage : Component
{
    public override VisualNode Render()
    {
        return new CustomContentPage()
        {
            new Button("Back")
                .VCenter()
                .HCenter()
                .OnClicked(GoBack)
        }
        .Set(MauiControls.NavigationPage.HasBackButtonProperty, true)
        .OnBackButtonPressed(OnBack)
        .Title("Child Page");
    }


    async void OnBack()
    {
        await ContainerPage.DisplayAlert("Hey", "hey", "cancel");
    }

    private async void GoBack()
    {
        await Navigation.PopAsync();
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://adospace.gitbook.io/mauireactor/components/navigation/back-button.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
