Xamarin 传递命令参数以删除列表视图条目
我有一个菜单项,可以删除ListView中的条目。我的问题是我传递的命令参数总是空的Xamarin 传递命令参数以删除列表视图条目,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一个菜单项,可以删除ListView中的条目。我的问题是我传递的命令参数总是空的 <StackLayout Orientation="Horizontal"> <Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalO
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
<Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
<Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding FullName}"/>
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding ContactNumber}"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
<MenuItem Text="Delete" IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>`
列表视图`
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
<Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
<Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding FullName}"/>
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding ContactNumber}"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
<MenuItem Text="Delete" IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>`
`
如果我将CommandParameter更改为
CommandParameter=“test”,测试字符串将正确通过。这里的问题是什么?您正在定义delete菜单项的XAML是
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
<Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
<Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding FullName}"/>
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding ContactNumber}"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
<MenuItem Text="Delete" IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>`
<MenuItem
Text="Delete"
IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}" />
由于它位于您的数据模板中
,ViewCell
的默认BindingContext
是与列表项关联的单个联系人
,其属性为全名
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
<Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
<Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding FullName}"/>
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding ContactNumber}"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
<MenuItem Text="Delete" IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>`
无论如何,您正在显式设置菜单项的BindingContext
。由于CommandParameter
已绑定到MenuItem
的BindingContext
,因此当BindingContext
更改时,XF将尝试更新CommandParameter
。但是由于deletemon
没有属性FullName
,因此CommandParameter
将为null
。如果您没有明确设置BindingContext
,它应该可以工作,因为在本例中,BindingContext
是父项之一,即您的联系人
您正在定义删除菜单项
的XAML是
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
<Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
<Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding FullName}"/>
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding ContactNumber}"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
<MenuItem Text="Delete" IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>`
<MenuItem
Text="Delete"
IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}" />
由于它位于您的数据模板中
,ViewCell
的默认BindingContext
是与列表项关联的单个联系人
,其属性为全名
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
<Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
<Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding FullName}"/>
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding ContactNumber}"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
<MenuItem Text="Delete" IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>`
无论如何,您正在显式设置菜单项的BindingContext
。由于CommandParameter
已绑定到MenuItem
的BindingContext
,因此当BindingContext
更改时,XF将尝试更新CommandParameter
。但是由于deletemon
没有属性FullName
,因此CommandParameter
将为null
。如果您没有显式设置BindingContext
,它应该可以工作,因为在本例中,BindingContext
是父对象之一,即您的联系人
,假设您的ListView中填充了一组视图模型,您可以通过以下方式引用调用MenuItem命令的模型:
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
<Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
<Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding FullName}"/>
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding ContactNumber}"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
<MenuItem Text="Delete" IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>`
<ViewCell.ContextActions>
<MenuItem Clicked="OnEdit" CommandParameter="{Binding .}" Text="Edit" IsDestructive="false" />
<MenuItem Clicked="OnDelete" CommandParameter="{Binding .}" Text="Löschen" IsDestructive="true" />
</ViewCell.ContextActions>
其中,“.”是listview项源的当前项的缩写(只需保留其中的点即可。看起来不正确,但有效)。假设listview中填充了一组视图模型,您可以通过以下方式引用调用MenuItem命令的模型:
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
<Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
<Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding FullName}"/>
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding ContactNumber}"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
<MenuItem Text="Delete" IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>`
<ViewCell.ContextActions>
<MenuItem Clicked="OnEdit" CommandParameter="{Binding .}" Text="Edit" IsDestructive="false" />
<MenuItem Clicked="OnDelete" CommandParameter="{Binding .}" Text="Löschen" IsDestructive="true" />
</ViewCell.ContextActions>
其中,“.”是listview项目源的当前项目的缩写(只需将点保留在那里即可。看起来不正确,但有效)。您的项目源在哪里,它里面是什么?我的ItemSource是一个列表联系人
,全名肯定在那里,因为点击手势识别器
使用绑定全名
也很有效,而不是为命令参数绑定全名,请尝试使用点“.CommandParameter=“{Binding.}”不幸的是,它仍然是空的您的项目资源在哪里
,里面是什么?我的项目源是一个列表联系人
,全名肯定在那里,因为点击手势识别器使用绑定全名
也可以很好地工作,而不是绑定到命令参数的全名,请尝试使用点“.”CommandParameter=“{Binding.}”,遗憾的是它仍然为空
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
<Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
<Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding FullName}"/>
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}"
CommandParameter="{Binding ContactNumber}"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
<MenuItem Text="Delete" IsDestructive="True"
Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding FullName}"
BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>`