This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Don't run Command CanExecute on incorrect inherited binding context type #572
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
461c343
Allow Command CanExecute to recover when run on inherited bindingcontext
hartez b8c7990
Make exception handler more generic
hartez 4fcb063
Checking types in Command delegates to avoid exception in the first p…
hartez 2b280bd
Adding type chekc to other Command constructor
hartez fc24b13
Use nameof for ArgumentNullExceptions
hartez e7ef612
Add unit tests for null parameters, handle value types and Nullable<T>
hartez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla47971.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
using Xamarin.Forms.CustomAttributes; | ||
using Xamarin.Forms.Internals; | ||
|
||
namespace Xamarin.Forms.Controls.Issues | ||
{ | ||
[Preserve(AllMembers = true)] | ||
[Issue(IssueTracker.Bugzilla, 47971, "UWP doesn't display list items when binding a CommandParameter to BindingContext in a DataTemplate and including a CanExecute method", PlatformAffected.WinRT)] | ||
public class Bugzilla47971 : TestContentPage | ||
{ | ||
protected override void Init() | ||
{ | ||
var viewModel = new _47971ViewModel(); | ||
|
||
var lv = new ListView { BindingContext = viewModel }; | ||
|
||
lv.SetBinding(ListView.ItemsSourceProperty, new Binding("Models")); | ||
lv.SetBinding(ListView.SelectedItemProperty, new Binding("SelectedModel")); | ||
|
||
lv.ItemTemplate = new DataTemplate(() => | ||
{ | ||
var tc = new TextCell(); | ||
|
||
tc.SetBinding(TextCell.TextProperty, new Binding("Name")); | ||
tc.SetBinding(TextCell.CommandParameterProperty, new Binding(".")); | ||
tc.SetBinding(TextCell.CommandProperty, new Binding("BindingContext.ModelSelectedCommand", source: lv)); | ||
|
||
return tc; | ||
}); | ||
|
||
var layout = new StackLayout { Spacing = 10 }; | ||
var instructions = new Label {Text = "The ListView below should display three items (Item1, Item2, and Item3). If it does not, this test has failed." }; | ||
|
||
layout.Children.Add(instructions); | ||
layout.Children.Add(lv); | ||
|
||
Content = layout; | ||
} | ||
|
||
[Preserve(AllMembers = true)] | ||
internal class _47971ViewModel : INotifyPropertyChanged | ||
{ | ||
_47971ItemModel _selectedModel; | ||
Command<_47971ItemModel> _modelSelectedCommand; | ||
ObservableCollection<_47971ItemModel> _models; | ||
|
||
public ObservableCollection<_47971ItemModel> Models | ||
{ | ||
get { return _models; } | ||
set | ||
{ | ||
_models = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public _47971ItemModel SelectedModel | ||
{ | ||
get { return _selectedModel; } | ||
set | ||
{ | ||
_selectedModel = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public Command<_47971ItemModel> ModelSelectedCommand => _modelSelectedCommand ?? | ||
(_modelSelectedCommand = new Command<_47971ItemModel>(ModelSelectedCommandExecute, CanExecute)); | ||
|
||
bool CanExecute(_47971ItemModel itemModel) | ||
{ | ||
return true; | ||
} | ||
|
||
void ModelSelectedCommandExecute(_47971ItemModel model) | ||
{ | ||
System.Diagnostics.Debug.WriteLine(model.Name); | ||
} | ||
|
||
public _47971ViewModel() | ||
{ | ||
_models = new ObservableCollection<_47971ItemModel>( | ||
new List<_47971ItemModel>() | ||
{ | ||
new _47971ItemModel() { Name = "Item 1"}, | ||
new _47971ItemModel() { Name = "Item 2"}, | ||
new _47971ItemModel() { Name = "Item 3"} | ||
}); | ||
} | ||
|
||
public event PropertyChangedEventHandler PropertyChanged; | ||
|
||
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) | ||
{ | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | ||
} | ||
} | ||
|
||
[Preserve(AllMembers = true)] | ||
internal class _47971ItemModel | ||
{ | ||
public string Name { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,59 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Windows.Input; | ||
|
||
namespace Xamarin.Forms | ||
{ | ||
public sealed class Command<T> : Command | ||
public sealed class Command<T> : Command | ||
{ | ||
public Command(Action<T> execute) : base(o => execute((T)o)) | ||
public Command(Action<T> execute) | ||
: base(o => | ||
{ | ||
if (IsValidParameter(o)) | ||
{ | ||
execute((T)o); | ||
} | ||
}) | ||
{ | ||
if (execute == null) | ||
throw new ArgumentNullException("execute"); | ||
{ | ||
throw new ArgumentNullException(nameof(execute)); | ||
} | ||
} | ||
|
||
public Command(Action<T> execute, Func<T, bool> canExecute) : base(o => execute((T)o), o => canExecute((T)o)) | ||
public Command(Action<T> execute, Func<T, bool> canExecute) | ||
: base(o => | ||
{ | ||
if (IsValidParameter(o)) | ||
{ | ||
execute((T)o); | ||
} | ||
}, o => IsValidParameter(o) && canExecute((T)o)) | ||
{ | ||
if (execute == null) | ||
throw new ArgumentNullException("execute"); | ||
throw new ArgumentNullException(nameof(execute)); | ||
if (canExecute == null) | ||
throw new ArgumentNullException("canExecute"); | ||
throw new ArgumentNullException(nameof(canExecute)); | ||
} | ||
|
||
static bool IsValidParameter(object o) | ||
{ | ||
if (o != null) | ||
{ | ||
// The parameter isn't null, so we don't have to worry whether null is a valid option | ||
return o is T; | ||
} | ||
|
||
var t = typeof(T); | ||
|
||
// The parameter is null. Is T Nullable? | ||
if (Nullable.GetUnderlyingType(t) != null) | ||
{ | ||
return true; | ||
} | ||
|
||
// Not a Nullable, if it's a value type then null is not valid | ||
return !t.GetTypeInfo().IsValueType; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd swap the 2 checks. first check if it's a reference type, and if it's not, start to wonder about Nullable... |
||
} | ||
} | ||
|
||
|
@@ -28,31 +65,31 @@ public class Command : ICommand | |
public Command(Action<object> execute) | ||
{ | ||
if (execute == null) | ||
throw new ArgumentNullException("execute"); | ||
throw new ArgumentNullException(nameof(execute)); | ||
|
||
_execute = execute; | ||
} | ||
|
||
public Command(Action execute) : this(o => execute()) | ||
{ | ||
if (execute == null) | ||
throw new ArgumentNullException("execute"); | ||
throw new ArgumentNullException(nameof(execute)); | ||
} | ||
|
||
public Command(Action<object> execute, Func<object, bool> canExecute) : this(execute) | ||
{ | ||
if (canExecute == null) | ||
throw new ArgumentNullException("canExecute"); | ||
throw new ArgumentNullException(nameof(canExecute)); | ||
|
||
_canExecute = canExecute; | ||
} | ||
|
||
public Command(Action execute, Func<bool> canExecute) : this(o => execute(), o => canExecute()) | ||
{ | ||
if (execute == null) | ||
throw new ArgumentNullException("execute"); | ||
throw new ArgumentNullException(nameof(execute)); | ||
if (canExecute == null) | ||
throw new ArgumentNullException("canExecute"); | ||
throw new ArgumentNullException(nameof(canExecute)); | ||
} | ||
|
||
public bool CanExecute(object parameter) | ||
|
@@ -73,8 +110,7 @@ public void Execute(object parameter) | |
public void ChangeCanExecute() | ||
{ | ||
EventHandler changed = CanExecuteChanged; | ||
if (changed != null) | ||
changed(this, EventArgs.Empty); | ||
changed?.Invoke(this, EventArgs.Empty); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if
o
isnull
?