Binding Command int Causes InvalidCastException

AdamFisherAdamFisher USMember ✭✭
edited December 2015 in Xamarin.Forms

I am attempting to use a Command to invoke an operation but always get an invalid cast exception. I am clearly passing a valid number (55) as shown in my XAML:

<Image Source="mybutton.png" HeightRequest="48" Aspect="AspectFit">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding MyCommand}" CommandParameter="55"/>
    </Image.GestureRecognizers>
</Image>

Here is the declaration of MyCommand:

public static readonly BindableProperty MyCommandProperty = BindableProperty.Create<MyAppView, ICommand>(p => p.MyCommand, null);

public ICommand MyCommand
{
    get { return (ICommand)GetValue(MyCommandProperty); }
    private set { SetValue(MyCommandProperty, value); }
}

Then in the constructor I am initializing it:

MyCommand = new Command<int>(
    (number) => DoCommand(number),
    (number) => CanDoCommand(number));

It appears to be something internal to Xamarin Forms since I don't see anything pointing to my code in the stack trace. The error also mentions an error related to mono. Any ideas on what might be causing the cast issue?

Unhandled Exception:

System.InvalidCastException: Specified cast is not valid.

2015-12-12 11:17:07.187 MyAppiOS[11105:204037] 
Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
  at Xamarin.Forms.Command`1+<>c__DisplayClass1_0[T].<.ctor>b__1 (System.Object o) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Command.CanExecute (System.Object parameter) [0x00008] in <filename unknown>:0 
  at Xamarin.Forms.TapGestureRecognizer.SendTapped (Xamarin.Forms.View sender) [0x00011] in <filename unknown>:0 
  at Xamarin.Forms.Platform.iOS.EventTracker+<>c__DisplayClass3_0.<GetNativeRecognizer>b__0 (UIKit.UITapGestureRecognizer r) [0x00045] in <filename unknown>:0 
  at UIKit.UITapGestureRecognizer+Callback.Activated (UIKit.UITapGestureRecognizer sender) [0x00000] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIGestureRecognizer.cs:211 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /U
sers/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:77 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:61 
  at MyApp.iOS.Application.Main (System.String[] args) [0x00001] in D:\Projects\MyApp\src\MyApp.iOS\Main.cs:12 
2015-12-12 11:17:07.187 MyAppiOS[11105:204037] Unhandled managed exception:
Specified cast is not valid. (System.InvalidCastException)
  at Xamarin.Forms.Command`1+<>c__DisplayClass1_0[T].<.ctor>b__1 (System.Object o) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Command.CanExecute (System.Object parameter) [0x00008] in <filename unknown>:0 
  at Xamarin.Forms.TapGestureRecognizer.SendTapped (Xamarin.Forms.View sender) [0x00011] in <filename unknown>:0 
  at Xamarin.Forms.Platform.iOS.EventTracker+<>c__DisplayClass3_0.<GetNativeRecognizer>b__0 (UIKit.UITap
GestureRecognizer r) [0x00045] in <filename unknown>:0 
  at UIKit.UITapGestureRecognizer+Callback.Activated (UIKit.UITapGestureRecognizer sender) [0x00000] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIGestureRecognizer.cs:211 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:77 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:61 
  at MyApp.iOS.Application.Main (System.String[] args) [0x00001] in D:\Projects\MyApp\src\MyApp.iOS\Main.cs:12 
2015-12-12 11:17:07.187 MyAppiOS[11105:204037] critical: Stacktrace:

2015-12-12 11:17:07.188 MyAppiOS[11105
:204037] critical: 
Native stacktrace:
2015-12-12 11:17:07.206 MyAppiOS[11105:204037] critical:    0   MyAppiOS                      0x000000010d8d47bd mono_handle_native_sigsegv + 253
2015-12-12 11:17:07.207 MyAppiOS[11105:204037] critical:    1   libsystem_platform.dylib            0x0000000116b5ef1a _sigtramp + 26
2015-12-12 11:17:07.207 MyAppiOS[11105:204037] critical:    2   ???                                 0x000000010de14010 0x0 + 4527833104
2015-12-12 11:17:07.207 MyAppiOS[11105:204037] critical:    3   libsystem_c.dylib                   0x00000001168eacec abort + 129
2015-12-12 11:17:07.207 MyAppiOS[11105:204037] critical:    4   MyAppiOS                      0x000000010da3e64a xamarin_unhandled_exception_handler + 298
2015-12-12 11:17:07.207 MyAppiOS[11105:204037] critical:    5   MyAppiOS                      0x000000010d8d4f83 mono_invoke_unhandled_exception_hook + 83
2015-12-12 11:17:07.208 MyAppiOS[11105:204037] critical:    6   MyAppiOS                      0x000000010d8d4022 mono_handle_exception_internal + 5362
2015-12-12 11:17:07.208 MyAppiOS[11105:204037] critical:    7   MyAppiOS                      0x000000010d8d2b28 mono_handle_exception + 24
2015-12-12 11:17:07.208 MyAppiOS[11105:204037] critical:    8   MyAppiOS                      0x000000010d84d091 mono_amd64_throw_exception + 113
2015-12-12 11:17:07.208 MyAppiOS[11105:204037] critical:    9   MyAppiOS                      0x000000010d84d112 mono_amd64_throw_corlib_exception + 82
2015-12-12 11:17:07.208 MyAppiOS[11105:204037] critical:    10  ???                                 0x0000000124de29a3 0x0 + 4913506723
2015-12-12 11:17:07.209 MyAppiOS[11105:204037] critical: 
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Best Answer

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    The XAML parser doesn't know what type your command parameter should be so without any type information it is going to assume it's a string. You may be able to use CommandParameter="{x:Int32 55}" to tell it that this is an int. I'm not certain that Forms supports that extension, and I'm not at a computer to check. It's worth a try.

  • PhilipOchuPhilipOchu USBeta, University ✭✭✭

    Confirmed - CommandParameter="{x:Int32 55}" works

  • AdamFisherAdamFisher USMember ✭✭

    Thanks for the responses guys. I tried the Int32 syntax and it wouldn't even load the page. It failed at the InitializeComponent() call. I ended up doing:

    MyCommand = new Command<string>(
        (number) => DoCommand(int.Parse(number)),
        (number) => CanDoCommand(int.Parse(number)));
    
  • adamkempadamkemp USInsider, Developer Group Leader mod

    Not sure why it would work for some but not others. What was the exact code you used, and what exception message did you get?

Sign In or Register to comment.