[英]Delete and change items in ListView ViewCells
In a project I am coding I am trying to make it so if a user clicks an options Icon it opens a menu where the user has the option to delete the post and it removes it from the List, I also want to make it so if users click the like button it changed the icon. 在一个我正在编码的项目中,我试图做到这一点,因此,如果用户单击选项图标,它将打开一个菜单,用户可以在其中删除该帖子,然后将其从列表中删除,我也想这样做用户单击“喜欢”按钮,它会更改图标。
My problem is, I can't find out how to get a variable attached to that ViewCell so in the method I can delete it or update the like button. 我的问题是,我找不到如何将变量附加到该ViewCell上,因此在该方法中,我可以删除它或更新like按钮。
My ListView is 我的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>
Then the code behind it 然后是其背后的代码
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; }
}
这会将选定的MessageObject作为参数传递给OptionClick命令
<TapGestureRecognizer Command="{Binding OptionClick}" CommandParameter="{Binding .}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.