在我的 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。