Xamarin 传递命令参数以删除列表视图条目

Xamarin 传递命令参数以删除列表视图条目,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一个菜单项,可以删除ListView中的条目。我的问题是我传递的命令参数总是空的 <StackLayout Orientation="Horizontal"> <Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalO

我有一个菜单项,可以删除ListView中的条目。我的问题是我传递的命令参数总是空的

                        <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>`