0
votes

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.

1
side note you don't need to wrap a collectionView inside a ScrollView, it handle scroll capability by itselfCfun
The code doesn't look wrong, and my testing with your code doesn't reproduce your issue,could you share a sample which the issue occurred ?Leo Zhu - MSFT
Thanks for the reply. @Cfun gonna try cleaning up the code a little bit. If I found a solution I will answer the question myselft.KadoLakatt

1 Answers

0
votes

The XAML markup of the ContentPage was defined like this:

<ContentPage.Content>
    <RefreshView>
        <Grid>
           <StackLayout>
               <ContentView>...</ContentView>
               <StackLayout>...</StackLayout> 
           </StackLayout> 
           <ScrollView>
               <CollectionView>...</CollectionView>
           </ScrollView>
        </Grid>
    </RefreshView>
</ContentPage.Content>

Starting with @Cfun recommendation:

Side note you don't need to wrap a collectionView inside a ScrollView, it handle scroll capability by itself.

So I started to cleanup the markup of the ContentPage, I remove unnecessary layout controls and then, the CollectionView and SwipeView inside started to working perfectly well. The markup just end up like this:

<ContentPage.Content>
        <StackLayout>
            <ContentView>
             .
             .
             .
            </ContentView>
            <CollectionView>
             .
             .
             .
            </CollectionView>
        </StackLayout> 
</ContentPage.Content>