这里我使用MVVM体系结构来开发应用程序。我在listview中有一个图像,并且listview绑定了一个列表(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类中调用一个命令,并且我需要通过这个调用发送一个Id。(命令、命令参数、绑定上下文(
<TapGestureRecognizer Command="{Binding Path=BindingContext.ChatTappedCommand, Source={x:Reference Name=MailboxAcceptedList}}"
CommandParameter="{Binding}"/>