Command Binding - System.InvalidCastException

Hallo,

if I add a  Binding, like Command="{Binding ClickFilePickerCommand}", I receive an System.InvalidCastException Error. If I take the Binding away, Buttton without error is showing

<buttons:SfButton Grid.Row="2" Text="Button" CornerRadius="20" Command="{Binding ClickFilePickerCommand}" Margin="10" HorizontalOptions="Center" />


Error Message:

- $exception {System.InvalidCastException: Specified cast is not valid.

   at Syncfusion.Maui.Core.ButtonBase.OnCommandPropertyChanged(BindableObject bindable, Object oldValue, Object newValue)

   at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)

   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)

   at Microsoft.Maui.Controls.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget)

   at Microsoft.Maui.Controls.BindingExpression.Apply(Object sourceObject, BindableObject target, BindableProperty property)

   at Microsoft.Maui.Controls.Binding.Apply(Object context, BindableObject bindObj, BindableProperty targetProperty, Boolean fromBindingContextChanged)

   at Microsoft.Maui.Controls.BindableObject.ApplyBindings(Boolean skipBindingContext, Boolean fromBindingContextChanged)

   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)

   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)

   at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)

   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()

   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()

   at Microsoft.Maui.Controls.View.OnBindingContextChanged()

   at Microsoft.Maui.Controls.Grid.OnBindingContextChanged()

   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)

   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)

   at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)

   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()

   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()

   at Microsoft.Maui.Controls.View.OnBindingContextChanged()

   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)

   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.TemplatedView.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)

   at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)

   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()

   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()

   at Microsoft.Maui.Controls.View.OnBindingContextChanged()

   at Microsoft.Maui.Controls.ContentView.OnBindingContextChanged()

   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)

   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)

   at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)

   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()

   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()

   at Microsoft.Maui.Controls.View.OnBindingContextChanged()

   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)

   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)

   at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)

   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()

   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()

   at Microsoft.Maui.Controls.View.OnBindingContextChanged()

   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)

   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)

   at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)

   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()

   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()

   at Microsoft.Maui.Controls.View.OnBindingContextChanged()

   at Microsoft.Maui.Controls.Grid.OnBindingContextChanged()

   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)

   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.TemplatedView.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)

   at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)

   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()

   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()

   at Microsoft.Maui.Controls.View.OnBindingContextChanged()

   at Microsoft.Maui.Controls.ContentView.OnBindingContextChanged()

   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)

   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.TemplatedPage.SetChildInheritedBindingContext(Element child, Object context)

   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)

   at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)

   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()

   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()

   at Microsoft.Maui.Controls.Page.OnBindingContextChanged()

   at Microsoft.Maui.Controls.ContentPage.OnBindingContextChanged()

   at Microsoft.Maui.Controls.BindableObject.BindingContextPropertyChanged(BindableObject bindable, Object oldvalue, Object newvalue)

   at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)

   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)

   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess)

   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value)

   at Microsoft.Maui.Controls.BindableObject.set_BindingContext(Object value)

   at CycleProCut.Views.EvaluateTrack..ctor(EvaluateViewModel vm) in C:\Users\markus\source\repos\CycleProCut\CycleProCut\Views\EvaluateTrack.xaml.cs:line 15

   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeServiceProviderEngine.<>c__DisplayClass4_0.<RealizeService>b__0(ServiceProviderEngineScope scope)

   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)

   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)

   at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType)

   at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass19_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0()

   at Microsoft.Maui.Controls.ElementTemplate.CreateContent()

   at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)

   at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()

   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers()

   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()

   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)

   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)

   at CycleProCut.Program.Main(String[] args) in C:\Users\markus\source\repos\CycleProCut\CycleProCut\Platforms\iOS\Program.cs:line 13} System.InvalidCastException



Thanks,


Markus


6 Replies

SV Swetha Vasanthakumar Syncfusion Team September 18, 2023 11:04 AM UTC

Hello Markus,

 

We have investigated the reported issue regarding Command Binding in SfChip, based on your query. However, we were unable to reproduce the reported issue on our end.

 

To assist you better, we have created a sample and attached a sample along with a code snippet for your reference:

 

 

Snippet:

XAML:

 <StackLayout>

                  <Label Text="{Binding Number, StringFormat='Value is {0}'}"

                          Margin="20"

                          FontSize="20"

                          VerticalOptions="Center"

                          HorizontalOptions="Center" />

       <buttons:SfButton Text="{Binding Number}"   

                          HeightRequest="50"

                          WidthRequest="100"

                          VerticalOptions="Center"

                          HorizontalOptions="Center"

                          Command="{Binding ButtonCommand}" />

        </StackLayout>

 

 

Could you please check the sample and If you still face any problems, please revert us by modifying the sample based on your application scenario or sharing the code snippet for custom filtering and item source used in your application. This will help us to assist you with a better solution as soon as possible.

 

Regards,

Swetha V



Attachment: SfButtonSample_5dd65647.zip


MN Markus Nottbohm September 19, 2023 11:14 AM UTC

Hallo,

I tested your code and it is working, when I use the  

public ICommand ButtonCommand { get; private set; }

but if I use the RelayCommand , from the CommunityToolkit, it throws the error above:


        [RelayCommand]

         async Task TrackAppearing()

        {

            await LoadTrackData();

        }


Thanks,


Markus



AK Arunachalam Kandasamy Raja Syncfusion Team September 22, 2023 08:01 AM UTC

Hi Markus,

 

We have validated the SfButton with RelayCommand and the reported issue was not reproduced on our end. To better understand the root cause and assist with resolving the issue, it would be helpful if you could provide more detailed information about the scenario in which our control was used, including a sample demonstrating the issue with replication steps. Please let us know if you have any other queries.

 

Kindly review the attached example and apply any required modifications to reproduce the problem. Afterward, kindly forward the sample to us, as it would enable us to conduct a more thorough investigation of the issue.

 

Regards,

Arunachalam K


Attachment: SfButtonSample184603_abd79890.zip


AD Adam Diament October 18, 2023 02:33 PM UTC

Hi team. I have the same problem. I have attached a zipped solution that replicates the problem using the latest version SyncFusion.Maui.Buttons 23.1.41


To replicate:


  1. Build and run the app.
  2. Click the bottom tab "two" to go to the page with the sfButton. Click the button a couple of times and see the output in the debug window. Wait one second or so.
  3. Click the bottom tab "one". Wait one or two seconds.
  4. Click the bottom tab "two" again - the exception happens.

Thanks!

Adam


Attachment: TestAsyncRelayCommand_51ec3942.zip



BV Brundha Velusamy Syncfusion Team October 19, 2023 12:46 PM UTC

Hi Adam,

 

We have checked your query and we are able to reproduce the reported issue "System.InvalidCastException thrown in SfButton" at our end. We have fixed the reported issue, at present, a custom patch has been created for version 23.1.41 for the core and button. We have attached both NuGet file for your convenience.

Please note that we have created this patch for version 23.1.41 specifically to resolve the issue reported in this ticket. If you have received other patches for the same version for other products, please apply all patches in the order received.

Note: Please refer to the below link to apply the custom patch.

https://www.syncfusion.com/kb/6987/how-to-clear-nuget-cache

We will include the fix in our upcoming weekly release on October 31, 2023. We appreciate your patience until then.

Disclaimer: The inclusion of this solution in the weekly release may change due to other factors, including but not limited to QA checks and work reprioritization.

 

Regards,

Brundha V


Attachment: CustomNuget_cad27343.zip


BV Brundha Velusamy Syncfusion Team October 31, 2023 09:44 AM UTC

Hi Adam,

 

Thanks for your patience.

 

We have included the fix for the reported issue in our latest weekly NuGet release, v23.1.43 which is available for download ( https://www.nuget.org/ ). We thank you for your support and appreciate your patience in waiting for this update. Please get in touch with us if you would require any further assistance.

 

Regards,

Brundha V


Loader.
Up arrow icon