在我正在编码的项目中我试图这样做如果用户单击选项图标它会打开一个菜单,用户可以选择删除帖子并将其从列表中删除,我也想制作因此,如果用户点击“赞”按钮,则会更改图标。
我的问题是,我无法找到如何获取附加到该ViewCell的变量,所以在方法中我可以删除它或更新类似按钮。
我的ListView是
<ListView x:Name="MessageView" HasUnevenRows="True" IsPullToRefreshEnabled="True" Refreshing="MessageView_Refreshing">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout x:Name="MessageLayout" BackgroundColor="White" Margin="10, 10, 10, 0" Padding="10, 10, 15, 10">
<Image Source="options_icon.png" HeightRequest="15" HorizontalOptions="End" Margin="0, 0, 10, 0">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OptionClick}"/>
</Image.GestureRecognizers>
</Image>
<Label Text="{Binding Body}" HorizontalOptions="CenterAndExpand" TextColor="Black" FontSize="15" Margin="0, 10, 0, 10"/>
<StackLayout x:Name="MessageFooter" Orientation="Horizontal">
<Image x:Name="LikeSource" Source="{Binding LikeImageSource}" HeightRequest="18" HorizontalOptions="StartAndExpand" Margin="0, 0, 10, 0">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding LikeClick}" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>
</Image>
<Label Text="{Binding Timestamp}" TextColor="Black" FontSize="10" HorizontalOptions="EndAndExpand"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
然后是它背后的代码
public IList<MessageObject> FormatPosts(Page page, INavigation navigation, string json)
{
IList<MessageObject> Posts = new List<MessageObject>() { };
var messages = JsonConvert.DeserializeObject<List<Message>>(json);
foreach (var message in messages)
{
DateTime dateFormat = Convert.ToDateTime(message.TimeStamp);
Posts.Add(new MessageObject
{
Body = message.Body,
OptionClick = new Command(() => ShowOptionActions(page, navigation, message.Id, message.Sender_Id)),
ImageSource = message.Liked == 0 ? "like_icon.png" : "liked_icon.png",
LikeClick = new Command(async (sender) => await LikeMessage(navigation, message.Sender_Id, sender)),,
Timestamp = dateFormat.ToString("MMMM dd, yyyy HH:mm")
});
}
return Posts;
}
public static async void ShowOptionActions(Page page, INavigation navigation, int id, int poster_id)
{
var action = await page.DisplayActionSheet("Message Actions", "Cancel", "Delete");
switch (action)
{
case "Delete":
await DeleteMessage(page, navigation, id);
break;
}
}
public static async Task LikeMessage(INavigation navigation, int id, object message)
{
*Updates database*
if (page_result.Equals("liked"))
{
((MessageObject)message).LikeImageSource = "liked_icon.png";
}
else if (page_result.Equals("unliked"))
{
((MessageObject)message).LikeImageSource = "like_icon.png"; // same as when you haven't liked it yet
}
}
public static async Task DeleteMessage(Page page, INavigation navigation, int id)
{
*Deletes from database*
//Delete post
return;
}
}
public class MessageObject
{
public string Body { get; set; }
public Command OptionClick { get; set; }
public Command LikeClick { get; set; }
public string Timestamp { get; set; }
public string ImageSource { get; set; }
}
答案 0 :(得分:1)
这会将选定的MessageObject作为参数传递给OptionClick命令
<TapGestureRecognizer Command="{Binding OptionClick}" CommandParameter="{Binding .}"/>