Вызов метода ViewModel из ListView.ItemTemplate в 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, а также мне нужно отправить идентификатор с этим вызовом. (Команда, CommandParameter, BindingContext)


person Rakesh Ravi G    schedule 14.06.2021    source источник