0

在我的 ContentPage XAML 文件中,我有一个 CollectionView 设置,如下所示:

<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 是一个 ObservableCollection。问题在 SwipeItems 中根深蒂固,Commands 执行良好,但 CommandParameter 有时会为 null,而有时会得到预期的绑定 TItem 对象。在 CommandParameter 属性上使用{Binding .}时的行为是相同的。

调试时,当返回空值时,我从 {Binding .} 转到 {Binding} 或反之亦然,它开始按预期工作。

命令在 viewModel 上声明如下:

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 被实例化并分配给 ContentPages 构造函数上的 BindingContext。

4

1 回答 1

0

ContentPage 的 XAML 标记定义如下:

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

从@Cfun 推荐开始:

旁注您不需要在 ScrollView 中包装 collectionView,它自己处理滚动功能。

所以我开始清理 ContentPage 的标记,我删除了不必要的布局控件,然后,里面的 CollectionView 和 SwipeView 开始工作得很好。标记最终是这样的:

<ContentPage.Content>
        <StackLayout>
            <ContentView>
             .
             .
             .
            </ContentView>
            <CollectionView>
             .
             .
             .
            </CollectionView>
        </StackLayout> 
</ContentPage.Content>
于 2021-03-01T15:47:35.450 回答