Skip to content

Use the shared CommandElement plumbing in BackButtonBehavior - #38412

Closed
wbv-profile wants to merge 1 commit into
dotnet:mainfrom
wbv-profile:fix/weak-events-command-content
Closed

wbv-profile wants to merge 1 commit into
dotnet:mainfrom
wbv-profile:fix/weak-events-command-content

Conversation

@wbv-profile

Copy link
Copy Markdown
Contributor

Description of Change

BackButtonBehavior subscribed to ICommand.CanExecuteChanged with a direct instance handler and only unsubscribed when Command was reassigned. A command is normally owned by a view model that outlives the Shell page it is bound from, so the subscription rooted every BackButtonBehavior ever bound to it.

Rather than add another bespoke proxy, this adopts the pattern the repo already has for exactly this problem: ICommandElement + CommandElement, which routes the subscription through WeakCommandSubscription. BackButtonBehavior already exposes Command and CommandParameter publicly, so implementing the interface is mostly deleting code — the hand-rolled OnCommandChanged, OnCommandParameterChanged and CanExecuteChanged are replaced by CommandElement.OnCommandChanging / OnCommandChanged / OnCommandParameterChanged and a single explicit ICommandElement.CanExecuteChanged.

This matches the direction of #38316, which is migrating SwipeItemView to the same interface.

Behaviour is preserved: IsEnabledCore is still driven by CanExecute, still re-evaluates when CommandParameter changes, and still returns to enabled when Command is cleared (CommandElement.GetCanExecute returns true for a null command, and OnCommandChanged invokes the handler after every change).

Worth knowing for review: MAUI's own Command already backs CanExecuteChanged with a WeakEventManager, so it never retained subscribers and this leak was invisible with it. The leak only bites with a hand-rolled ICommand that uses a plain CLR event — which is what most app-side RelayCommand implementations are. The test below uses such a command deliberately; a first version written against Command passed even against the unfixed code and would have proved nothing.

Tests

src/Controls/tests/Core.UnitTests/BackButtonBehaviorCommandTests.cs adds six tests. Reverting the source change fails exactly one — SharedCommandDoesNotRootBehavior, the leak, using the plain-event command. The other five pin behaviour the refactor must preserve: IsEnabled reflecting CanExecute on assignment, tracking ChangeCanExecute, tracking a plain-event command's CanExecuteChanged (which proves the weak subscription still delivers), re-evaluating on CommandParameter change, and returning to enabled when the command is cleared.

With the fix all six pass and the full Controls.Core.UnitTests suite is green (5653 passed, 0 failed).

Issues Fixed

Fixes #37635
Fixes #38175

BackButtonBehavior subscribed to ICommand.CanExecuteChanged with a
direct instance handler, unsubscribing only when Command was
reassigned. A command is normally owned by a view model that outlives
the Shell page, so the subscription rooted every BackButtonBehavior
ever bound to it.

Rather than add another bespoke proxy, this adopts ICommandElement /
CommandElement, which routes the subscription through
WeakCommandSubscription -- the same migration dotnet#38316 is making for
SwipeItemView. The hand-rolled OnCommandChanged,
OnCommandParameterChanged and CanExecuteChanged all go away.

Behaviour is preserved: IsEnabledCore is still driven by CanExecute,
still re-evaluates on CommandParameter change, and still returns to
enabled when Command is cleared.

Note MAUI's own Command backs CanExecuteChanged with a
WeakEventManager, so it never retained subscribers; the leak only
appears with a hand-rolled ICommand using a plain CLR event, which is
what the regression test uses.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 38412

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 38412"

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Sep 10, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@wbv-profile! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @wbv-profile! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@kubaflo

kubaflo commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Closing as overlapping with #36370, already merged into inflight/current as 8dddce7.

The approach is sound, but inflight/current already has this same ICommandElement implementation, the three shared CommandElement callbacks, CleanupTracker, and GetCanExecute call. The product-code differences are only comments, formatting and member order. Existing CommandsSubscribedToCanExecuteCollect coverage includes BackButtonBehavior with both weak-event and plain-event commands.

The additional IsEnabled behavior tests are useful and can be proposed as a test-only follow-up. main still lacks the implementation; if it needs a backport, please reuse the existing fix rather than introduce a parallel one.

@kubaflo kubaflo closed this Sep 10, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 27, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — dce22b61 Deployed Sep 10, 2026 by wbv-profile via conclusion #1847
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

community ✨ Community Contribution

Projects

None yet

2 participants