Repository navigation
Use the shared CommandElement plumbing in BackButtonBehavior - #38412
wbv-profile wants to merge 1 commit into
Conversation
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>
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 38412Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 38412" |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
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. |
|
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. |
|
Closing as overlapping with #36370, already merged into The approach is sound, but The additional |
Description of Change
BackButtonBehaviorsubscribed toICommand.CanExecuteChangedwith a direct instance handler and only unsubscribed whenCommandwas reassigned. A command is normally owned by a view model that outlives the Shell page it is bound from, so the subscription rooted everyBackButtonBehaviorever 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 throughWeakCommandSubscription.BackButtonBehavioralready exposesCommandandCommandParameterpublicly, so implementing the interface is mostly deleting code — the hand-rolledOnCommandChanged,OnCommandParameterChangedandCanExecuteChangedare replaced byCommandElement.OnCommandChanging/OnCommandChanged/OnCommandParameterChangedand a single explicitICommandElement.CanExecuteChanged.This matches the direction of #38316, which is migrating
SwipeItemViewto the same interface.Behaviour is preserved:
IsEnabledCoreis still driven byCanExecute, still re-evaluates whenCommandParameterchanges, and still returns to enabled whenCommandis cleared (CommandElement.GetCanExecutereturnstruefor a null command, andOnCommandChangedinvokes the handler after every change).Worth knowing for review: MAUI's own
Commandalready backsCanExecuteChangedwith aWeakEventManager, so it never retained subscribers and this leak was invisible with it. The leak only bites with a hand-rolledICommandthat uses a plain CLR event — which is what most app-sideRelayCommandimplementations are. The test below uses such a command deliberately; a first version written againstCommandpassed even against the unfixed code and would have proved nothing.Tests
src/Controls/tests/Core.UnitTests/BackButtonBehaviorCommandTests.csadds 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:IsEnabledreflectingCanExecuteon assignment, trackingChangeCanExecute, tracking a plain-event command'sCanExecuteChanged(which proves the weak subscription still delivers), re-evaluating onCommandParameterchange, and returning to enabled when the command is cleared.With the fix all six pass and the full
Controls.Core.UnitTestssuite is green (5653 passed, 0 failed).Issues Fixed
Fixes #37635
Fixes #38175