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

问题描述

这里我使用 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,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 类中调用一个 Command,并且我还需要通过这个调用发送一个 Id。 (命令、命令参数、绑定上下文)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)