c# - ListView 上下文菜单上的命令绑定(bind)未触发(未找到)?

标签 c# xamarin mvvm command

我的 ListView 上有绑定(bind)问题,我收到错误:

Binding: 'OnEdit' property not found on 'ContactsViewModel', target property: 'Xamarin.Forms.MenuItem.Command'

这是 XAML(也许我在引用时犯了错误):

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Contactium.ContactsPage"
         x:Name="ContactsPageContent">
<ContentPage.ToolbarItems>
...
<TextCell.ContextActions>
      <MenuItem  Command="{Binding Path=BindingContext.OnEdit, Source={x:Reference ContactsPageContent}}" CommandParameter="{Binding .}" Text="EDITER" IsDestructive="True"/>
      <MenuItem Command="{Binding Path=BindingContext.OnDelete, Source={x:Reference ContactsPageContent}}}" CommandParameter="{Binding .}" Text="SUPPRIMER"/>
</TextCell.ContextActions>
...

这是 ViewModel(ContactsPageContent):

public Command OnEdit(object sender, EventArgs e)
{
    return new Command(() =>
    {
         Debug.Write("OK");
    });
}

public Command OnDelete(object sender, EventArgs e)
{
    return new Command(() =>
    {
         Debug.Write("OK");
    });
}

感谢您的宝贵时间!

最佳答案

<ListView x:Name="Cities" <ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <ViewCell.ContextActions>

        <MenuItem Command="{Binding Path=BindingContext.DeleteCommand ,Source={x:Reference Name=Cities}}" CommandParameter="{Binding .}" Text="Delete" IsDestructive="True">

        </MenuItem>

      </ViewCell.ContextActions>

在你的 ViewModel 中你可以使用这个:

DeleteCommand = new Command<Category>(async (selected) =>
            {});

关于c# - ListView 上下文菜单上的命令绑定(bind)未触发(未找到)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53932467/

相关文章:

c# - 从 Visual Studio 数据库项目生成 Entity Framework 模型

javascript - 如何使用默认值创建 JavaScript 对象字段? (AngularJS模型相关)

wpf - 什么是分层面板的巧妙设计?

ios - 如何使用 rxswift 将图像绑定(bind)到 UIImageView?

c# - 将时间转换为特定时区然后转换为 UTC 时如何获得偏移量? C#

c# - listener.AcceptTcpClient() 在 c# 中不起作用,在 Java 中起作用

c# - 跟踪对象的关系列表的最佳方法是什么

xamarin - 如何在xamarin表单中使用X和Y坐标定位按钮?

android - NFC-V "Tag Lost"Xamarin 和 ST M24LR 标签异常

xamarin - 使用自定义渲染器,我可以使TableSection.Title以小写混合大小写出现吗?