Skip to content

Commit fabae3b

Browse files
Fixed CI failure : Restore BackButtonBehavior IsEnabled after CanExecute changes (#34668)
<!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue details This PR #28734 causes the unit test below 1. BackButtonUpdatesWhenSetToNewCommand 2. BackButtonDisabledWhenCommandDisabled ### Description of Change The root cause is [IsEnabledCore { set => SetValue(IsEnabledProperty, value && IsEnabled); }](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) — a circular read: once [IsEnabledProperty](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) is false, reading [IsEnabled](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) always returns false, so the property is permanently stuck. The fix introduces two backing fields to track the concerns independently: - [_userDefinedIsEnabled](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) — written only by the developer via [IsEnabled](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) - [_commandEnabled](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) — written only by [CanExecute](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) via [IsEnabledCore](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) - [IsEnabledProperty](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) is always [_userDefinedIsEnabled && _commandEnabled](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html), eliminating the circular read. - BackButtonDisabledWhenCommandDisabled — When [CanExecute](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) flips to true and [ChangeCanExecute()](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) is called, [_commandEnabled](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) updates correctly and the back button re-enables. - BackButtonUpdatesWhenSetToNewCommand — Setting [Command = null](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) resets [_commandEnabled = true](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html), so the back button re-enables as expected. ### Test fixed Unit tests: BackButtonUpdatesWhenSetToNewCommand BackButtonDisabledWhenCommandDisabled
1 parent 8896c40 commit fabae3b

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Controls/src/Core/Shell/BackButtonBehavior.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,22 @@ public ImageSource IconOverride
6262
set { SetValue(IconOverrideProperty, value); }
6363
}
6464

65+
// Tracks the value explicitly set by the user (default matches IsEnabledProperty default of true).
66+
bool _userDefinedIsEnabled = true;
67+
// Tracks the enabled state derived from the command's CanExecute result.
68+
bool _commandEnabled = true;
69+
6570
/// <summary>
6671
/// Gets or sets a value indicating whether the back button is enabled. This is a bindable property.
6772
/// </summary>
6873
public bool IsEnabled
6974
{
7075
get { return (bool)GetValue(IsEnabledProperty); }
71-
set { SetValue(IsEnabledProperty, value); }
76+
set
77+
{
78+
_userDefinedIsEnabled = value;
79+
SetValue(IsEnabledProperty, _userDefinedIsEnabled && _commandEnabled);
80+
}
7281
}
7382

7483
/// <summary>
@@ -89,7 +98,14 @@ public string TextOverride
8998
set { SetValue(TextOverrideProperty, value); }
9099
}
91100

92-
bool IsEnabledCore { set => SetValue(IsEnabledProperty, value && IsEnabled); }
101+
bool IsEnabledCore
102+
{
103+
set
104+
{
105+
_commandEnabled = value;
106+
SetValue(IsEnabledProperty, _userDefinedIsEnabled && value);
107+
}
108+
}
93109

94110
static void OnCommandChanged(BindableObject bindable, object oldValue, object newValue)
95111
{

0 commit comments

Comments
 (0)