From 368a375f13fc2e4b5ff00dbbdd7a2d6bb53988eb Mon Sep 17 00:00:00 2001 From: Paul DiPietro Date: Mon, 12 Sep 2016 06:51:19 -0500 Subject: [PATCH] [C] Prevent enabling a Button via setting a Command (#308) --- .../Bugzilla43354.cs | 56 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Core/Button.cs | 4 +- 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla43354.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla43354.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla43354.cs new file mode 100644 index 000000000..6ed77b961 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla43354.cs @@ -0,0 +1,56 @@ +using System; + +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 43354, "Button command being set after IsEnabled enables the button", PlatformAffected.All)] + public class Bugzilla43354 : TestContentPage + { + protected override void Init() + { + var buttonIsEnabledSetFirst = new Button + { + Text = "Click to display an alert", + IsEnabled = false, + Command = new Command(() => DisplayAlert("Test", "Message", "Cancel")), + }; + + var buttonIsEnabledSetSecond = new Button + { + Text = "Click to enable/disable button", + Command = new Command(() => + { + if (buttonIsEnabledSetFirst.IsEnabled) + buttonIsEnabledSetFirst.IsEnabled = false; + else + buttonIsEnabledSetFirst.IsEnabled = true; + }) + }; + + var buttonSetCommandToNull = new Button + { + Text = "Click to set first button's command to null", + Command = new Command(() => buttonIsEnabledSetFirst.Command = null) + }; + + Content = Content = new StackLayout + { + VerticalOptions = LayoutOptions.Center, + Children = + { + buttonIsEnabledSetFirst, + buttonIsEnabledSetSecond, + buttonSetCommandToNull + } + }; + } + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index adf173df4..1e0a2732e 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -124,6 +124,7 @@ + diff --git a/Xamarin.Forms.Core/Button.cs b/Xamarin.Forms.Core/Button.cs index 315730760..a4dffc781 100644 --- a/Xamarin.Forms.Core/Button.cs +++ b/Xamarin.Forms.Core/Button.cs @@ -180,7 +180,7 @@ namespace Xamarin.Forms void CommandCanExecuteChanged(object sender, EventArgs eventArgs) { ICommand cmd = Command; - if (cmd != null) + if (cmd != null && IsEnabled) IsEnabledCore = cmd.CanExecute(CommandParameter); } @@ -225,8 +225,6 @@ namespace Xamarin.Forms Command.CanExecuteChanged += CommandCanExecuteChanged; CommandCanExecuteChanged(this, EventArgs.Empty); } - else - IsEnabledCore = true; } void OnSourceChanged(object sender, EventArgs eventArgs)