enisn / Xamarin.Forms.InputKit

CheckBox, Radio Button, Labeled Slider, Dropdowns etc.
MIT License
589 stars 100 forks source link

Assigning Delegate Command to CheckedChangeCommand Crashing application, "Invalid cast" #220

Open logeshpalani33 opened 3 years ago

logeshpalani33 commented 3 years ago

Describe the bug Assigning Delegate Command to CheckedChangeCommand Crashing application, "Invalid cast" To Reproduce Steps to reproduce the behavior:

  1. Create new Checkbox
  2. Assign Delegate Command from Prism Framework
  3. and Run application
  4. See application will crash

Expected behavior Should raise the DelegateCommand Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

enisn commented 3 years ago

CheckChangedCommand is type of ICommand.

So, CheckChangedCommandProperty casts value to ICommand when parameter is set: https://github.com/enisn/Xamarin.Forms.InputKit/blob/af149c3ab9f28a69de6c0e57ef05482411f1cb0a/InputKit/Shared/Controls/CheckBox.cs#L225

I'm not sure why it doesn't work. I can try to reproduce later.

enisn commented 3 years ago

I've found a note in prism documentation

The DelegateCommand deliberately prevents the use of value types (int, double, bool, etc). Because ICommand takes an object, having a value type for T would cause unexpected behavior when CanExecute(null) is called during XAML initialization for command bindings. Using default(T) was considered and rejected as a solution because the implementor would not be able to distinguish between a valid and defaulted values. If you wish to use a value type as a parameter, you must make it nullable by using DelegateCommand<Nullable> or the shorthand ? syntax (DelegateCommand<int?>).

If you're using bool, you can try to use bool?

logeshpalani33 commented 3 years ago

okey will try and let you know