Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[C] Prevent enabling a Button via setting a Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul DiPietro committed Aug 16, 2016
1 parent e488f7f commit a7ca96a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla42075.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla42329.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla42364.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla43354.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CarouselAsync.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla34561.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla34727.cs" />
Expand Down
4 changes: 1 addition & 3 deletions Xamarin.Forms.Core/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected override void OnPropertyChanging(string propertyName = null)
void CommandCanExecuteChanged(object sender, EventArgs eventArgs)
{
ICommand cmd = Command;
if (cmd != null)
if (cmd != null && (bool)GetValue(IsEnabledProperty))
IsEnabledCore = cmd.CanExecute(CommandParameter);
}

Expand Down Expand Up @@ -213,8 +213,6 @@ void OnCommandChanged()
Command.CanExecuteChanged += CommandCanExecuteChanged;
CommandCanExecuteChanged(this, EventArgs.Empty);
}
else
IsEnabledCore = true;
}

void OnSourceChanged(object sender, EventArgs eventArgs)
Expand Down

0 comments on commit a7ca96a

Please sign in to comment.