【问题标题】:Command binding on ListView context menu not firing (not found)?ListView 上下文菜单上的命令绑定未触发(未找到)?
【发布时间】:2019-05-24 17:46:30
【问题描述】:

我的 ListView 出现绑定问题,出现错误:

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");
    });
}

感谢您的宝贵时间!

【问题讨论】:

  • 尝试这样写你的命令public ICommand OnEdit { get; set; } OnEdit= new Command(EditAction); private void EditAction(object obj) { Debug.Write("OK"); }
  • 它有效,我必须使用属性所以,谢谢!

标签: c# xamarin mvvm command


【解决方案1】:

<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) =>
            {});

【讨论】:

    【解决方案2】:

    以如下属性的形式写出您的Command

    public ICommand OnEdit { get; set; }
    OnEdit= new Command(EditAction); 
    private void EditAction(object obj)
    { 
     Debug.Write("OK"); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      相关资源
      最近更新 更多