Xamarin.Forms 中 ListView.ItemTemplate 的 ViewModel 命令调用

ViewModel Command call from ListView.ItemTemplate in Xamarin.Forms

这里我使用MVVM架构开发应用。我在列表视图中有一个图像,列表视图与列表 (ViewAcceptedList) 绑定。在这里,我对图像使用 TapGestureRecognizer。在图像中,我需要调用一个命令 (ChatTappedCommand)。我正在分享我的代码,我是如何尝试达到要求的。我需要有经验的人的帮助。

<?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:IC="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin" 
                 xmlns:Custom="clr-namespace:MTR.Customization" xmlns:pv="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView" xmlns:local="clr-namespace:MTR.Views"
                 mc:Ignorable="d"
                 x:Class="MTR.Views.MailBox.MailBoxPage"
                 xmlns:behaviors="clr-namespace:MTR.ViewModels.Behavior" xmlns:VM="clr-namespace:MTR.ViewModels"
                 BackgroundColor="#F8F0F0">
    
        <ContentPage.BindingContext>
            <VM:MailBoxPageViewModel/>
        </ContentPage.BindingContext>
        
        <ContentPage.Content>
    
                <ListView x:Name="MailboxAcceptedList" 
                        IsVisible="{Binding ViewAcceptedList}"
                        ItemsSource="{Binding MailedBoxAcceptedList}"
                        SeparatorColor="Transparent" 
                        VerticalScrollBarVisibility="Never"
                        HasUnevenRows="True"
                        RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=stackNavigationBar, Property=Height,Constant=2, Factor=2.9}"
                        RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=.8}"
                        RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}">
                            
                <ListView.Behaviors>
                    <behaviors:MailBoxListViewBehavior />
                </ListView.Behaviors>
    
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
    
                            <Image  x:Name="imgChat"
                                Source="chat_32px.png"
                                HeightRequest="30"
                                WidthRequest="30">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer BindingContext="{Binding Source={x:Reference MailBoxPage}}" Command="{Binding ChatTappedCommand}"
                                                                                CommandParameter="{Binding MTRID}" />
                                    <!--Command="{Binding Source={x:Reference Name=nameYourPage}, Path=BindingContext. EditarEnderecoCommand}" CommandParameter="{Binding CliEndCep}"
                                                        Command="{Binding ChatTappedCommand}"
                                                        CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"-->
                                </Image.GestureRecognizers>
                            </Image>
                                        
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>`enter code here`
    
</ContentPage.Content>


//ViewModel:


using MTR.Models.Interface;
using MTR.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;

namespace MTR.ViewModels
{
    public class MailBoxPageViewModel : BindableObject
    {
        private ObservableCollection<MTR.Models.DbModel.Mail> mailedAcceptedList;
        public ObservableCollection<MTR.Models.DbModel.Mail> MailedBoxAcceptedList
        {
            get => mailedAcceptedList;
            set
            {
                if (value == mailedAcceptedList)
                    return;

                mailedAcceptedList = value;
                OnPropertyChanged();
            }
        }
       
        public ICommand ChatTappedCommand
        {
            get;
            private set;
        }
        private void ChatTapped(object obj)
        {

        }

        public MailBoxPageViewModel()
        {
            ChatTappedCommand = new Command(ChatTapped);

            MailedBoxAcceptedList = new ObservableCollection<Models.DbModel.Mail>();
        }
        
    }
}

在这里,我试图在 ViewModel class 中调用命令,并且我还需要通过此调用发送一个 Id。 (命令、命令参数、绑定上下文)

<TapGestureRecognizer Command="{Binding Path=BindingContext.ChatTappedCommand, Source={x:Reference Name=MailboxAcceptedList}}"
                                                          CommandParameter="{Binding}"/>