Getting exception like System.InvalidCastException on EventToCommandBehavior

I have ListView in my xamarin.forms project and here is the xaml code:

ListView ItemsSource="{Binding List}" x:Name="ListView">
      <ListView.Behaviors>
        <behav:EventToCommandBehavior EventName="ItemSelected" Command="{Binding DetailCommand}" CommandParameter="{Binding SelectedItem, Source={Reference ListView}}" />
      </ListView.Behaviors>
      <ListView.ItemTemplate>
        <DataTemplate>
          <ImageCell ImageSource="{Binding Logo}"
               Text="{Binding Name}" />
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

When I select an item from the list I am getting exception like System.InvalidCastException and it is happening only for IOS project but DROID project is working good without any issue. While getting the exception the cursor stands on this part of code:

if (Command.CanExecute(resolvedParameter))
            {
                Command.Execute(resolvedParameter);
            }

and this code is the part of below method:

void OnEvent(object sender, object eventArgs)
        {
            if (Command == null)
            {
                return;
            }

            object resolvedParameter;
            if (CommandParameter != null)
            {
                resolvedParameter = CommandParameter;
            }
            else if (Converter != null)
            {
                resolvedParameter = Converter.Convert(eventArgs, typeof(object), null, null);
            }
            else
            {
                resolvedParameter = eventArgs;
            }

            if (Command.CanExecute(resolvedParameter))
            {
                Command.Execute(resolvedParameter);
            }
        }

and above OnEvent method belongs to EventToCommandBehavior class.

Can anybody help me why I am getting this exception?

Posts

  • NMackayNMackay GBInsider, University mod
    edited October 2016

    @ShahriatHossain

    Maybe try setting the behavior inside the ImageCell like in the sample I posted here, I haven't used corcav's library for a while so I'm not sure if the syntax in my post is valid.

    https://forums.xamarin.com/discussion/60937/how-to-bind-a-command-to-listview-itemtapped

    There's also a good library from one of the Xamarin guys that I use
    https://www.nuget.org/packages/Behaviors.Forms/

    <ListView x:Name="ListviewOrders"
                      Grid.Row="1" SeparatorColor="Black"
                      ItemsSource="{Binding Orders}"
                      SelectedItem="{Binding SelectedOrder}"
                      VerticalOptions="Fill">
    
                <ListView.Behaviors>
                    <b:EventHandlerBehavior EventName="ItemTapped">
                        <b:InvokeCommandAction Command="{Binding SelectCommand}"
                                               Converter="{StaticResource SelectedltemArgsToBinding}" />
                    </b:EventHandlerBehavior>
                </ListView.Behaviors>
    

    Both libraries should do the job though.

Sign In or Register to comment.