Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Button): Command binding resetting on CanExecution exception (backport #18378) #18388

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Controls.Primitives;
using Color = Windows.UI.Color;
using Microsoft.UI.Xaml.Data;

#if HAS_UNO_WINUI || WINAPPSDK || WINUI
using Colors = Microsoft.UI.Colors;
Expand Down Expand Up @@ -318,6 +319,26 @@ public async Task When_Button_Flyout_TemplateBinding()
}
#endif

[TestMethod]
public async Task When_Command_CanExecute_Throws()
{
// Here we are testing against a bug where
// a data-bound ICommand that throws in its CanExecute
// can cause the binding to reset to its FallbackValue.
var vm = new
{
UnstableCommand = new DelegateCommand(x => throw new Exception("fail...")),
};

var sut = new Button();
sut.SetBinding(Button.CommandProperty, new Binding { Path = new(nameof(vm.UnstableCommand)), FallbackValue = new NoopCommand() });
sut.DataContext = vm;

await UITestHelper.Load(sut, x => x.IsLoaded);

Assert.AreEqual(vm.UnstableCommand, sut.Command, "Binding did not set the proper value.");
}

private async Task RunIsExecutingCommandCommon(IsExecutingCommand command)
{
void FocusManager_LosingFocus(object sender, LosingFocusEventArgs e)
Expand Down Expand Up @@ -422,5 +443,26 @@ public void Execute(object parameter)
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}

public class DelegateCommand : ICommand
{
private readonly Func<object, bool> canExecuteImpl;
private readonly Action<object> executeImpl;

public event EventHandler CanExecuteChanged;

public DelegateCommand(Func<object, bool> canExecute = null, Action<object> execute = null)
{
this.canExecuteImpl = canExecute;
this.executeImpl = execute;
}

public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, default);

public bool CanExecute(object parameter) => canExecuteImpl?.Invoke(parameter) ?? true;
public void Execute(object parameter) => executeImpl?.Invoke(parameter);
}

public class NoopCommand() : DelegateCommand(null, null) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Windows.Input;
using Uno.Disposables;
using Uno.Foundation.Logging;
using Uno.UI.Xaml.Core;
using Windows.Devices.Input;
using Windows.Foundation;
Expand Down Expand Up @@ -86,7 +87,7 @@ internal override void OnPropertyChanged2(DependencyPropertyChangedEventArgs arg
}
else if (args.Property == CommandParameterProperty)
{
UpdateCanExecute();
UpdateCanExecuteSafe();
}
else if (args.Property == VisibilityProperty)
{
Expand Down Expand Up @@ -273,7 +274,7 @@ void CanExecuteChangedHandler(object? sender, object args)
}

// Coerce the button enabled state with the CanExecute state of the command.
UpdateCanExecute();
UpdateCanExecuteSafe();
}

/// <summary>
Expand All @@ -297,6 +298,24 @@ private void UpdateCanExecute()
SuppressIsEnabled(suppress);
}

private void UpdateCanExecuteSafe()
{
// uno specific workaround:
// If Button::Command binding produces an ICommand value that throws Exception in its CanExecute,
// this value will be canceled and replaced by the Binding::FallbackValue.
try
{
UpdateCanExecute();
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().Error($"Failed to update CanExecute", e);
}
}
}

/// <summary>
/// Executes ButtonBase.Command.
/// </summary>
Expand Down
Loading