In my ContentPage XAML file, I have a CollectionView setup like this:
<ScrollView Grid.Row="1" Padding="0,0,0,0" BackgroundColor="LightGray" IsVisible="{ Binding ShowTrip }" >
<CollectionView x:Name="collectionView"
ItemsSource="{Binding Items}"
SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem Text="Directions"
IconImageSource="icon_directions.png"
BackgroundColor="White"
Command="{Binding Source={x:Reference myContentPage}, Path=BindingContext.DirectionsCommand}"
CommandParameter="{Binding}"/>
<SwipeItem Text="Deliver"
IconImageSource="icon_deliver.png"
BackgroundColor="White"
Command="{Binding Source={x:Reference myContentPage}, Path=BindingContext.DeliverCommand}"
CommandParameter="{Binding}"/>
</SwipeItems>
</SwipeView.RightItems>
<Frame BackgroundColor="White" CornerRadius="0" Margin="0,5,0,0" HasShadow="True">
<Grid x:DataType="model:sRemesa" Padding="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Text="Remittance" LineBreakMode="NoWrap" FontAttributes="Bold"
Style="{DynamicResource ListItemTextStyle}"
FontSize="13" Grid.Column="0" Grid.Row="1" /> <Label Text="{Binding Number}" LineBreakMode="NoWrap"
Style="{DynamicResource ListItemTextStyle}"
FontSize="13" Grid.Column="1" Grid.Row="1" />
<Label Text="{Binding Customer}"
Grid.Row="2"
LineBreakMode="NoWrap" TextTransform="Uppercase"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13" Grid.ColumnSpan="4" />
<Label Text="{Binding Address }"
Grid.Row="3"
LineBreakMode="NoWrap" TextTransform="Uppercase"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13" Grid.ColumnSpan="4" />
<Label Text="Boxes" FontAttributes="Bold"
Grid.Row="4" Grid.Column="0"
LineBreakMode="NoWrap"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13" />
<Label Text="{Binding BoxesQty}"
Grid.Row="4"
LineBreakMode="NoWrap" TextTransform="Uppercase"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13" Grid.Column="1" />
<Label Text="Weight" FontAttributes="Bold"
Grid.Row="4"
LineBreakMode="NoWrap"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13" Grid.Column="2" />
<Label Text="{Binding TotalWeight }"
Grid.Row="4"
LineBreakMode="NoWrap" TextTransform="Uppercase"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13" Grid.Column="3" />
</Grid>
</Frame>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ScrollView>
Items is an ObservableCollection. The problem radicates in the SwipeItems, Commands are executing well, but CommandParameter sometimes gets null and some other times got the expected bound TItem object. The behavior is the same when using {Binding .} on CommandParameter property.
When debugging, when null value is being returned I go from {Binding .} to {Binding} or viceversa, and it starts working as expected.
Commands are declared on viewModel like this:
public ICommand DeliverCommand => new Command<TItem>(DeliverRemittance);
public ICommand DirectionsCommand => new Command<TItem>(DirectionsToDestination);
private async void DirectionsToDestination(TItem item)
{
//....
}
private async void DeliverRemittance(TItem item)
{
//....
}
ViewModel is instantiated and assigned to BindingContext on the ContentPages constructor.