问题
Recently I have posted a question related to flowlistview
item visibility, this question is also related to that.
I have 8 images in a list, initially show a question mark image. When tap on the question mark image, the real image will visible under the question mark image. Implemented this feature using the recent thread.
This is a game for kids. Game rule: The player first taps one image, then the real image is visible to the player. Then the player taps another image, then that real image also visible to the player now. If the images match the player gets points and both selected items will be removed from the UI. If not match again hide the real image with question mark image.
I have done the above things, but the problem is sometimes the selection of images is wrong. If I select the fourth image, the image under the second or sixth is showing in UI. Don't know what is the reason behind this. I have uploaded a sample project here.
回答1:
Cause: You matched the pictures by determining whether their imageUrl are equal . But the imageUrl is not unique in the List .
Solution: You could add a property Id in the model .
public int Id { get; set; }
And handle the logic by determining if item's Id are equal .
ImageItems.Add(new NameMatchList() { Id = 1,imageUrl = "/cbrain-app/files/doc-lib/2018/02/22/11/46/06/971/head/Comfort the Sorrowing.png" });
ImageItems.Add(new NameMatchList() { Id = 2, imageUrl = "/cbrain-app/files/doc-lib/2018/02/22/11/46/23/784/head/Giving Food To The Hungry.png" });
//...
if (items.Id == selecteditem.Id)
{
if (!firstImageFlipped)
{
items.ImageVisibility = true;
items.TopImageVisibility = false;
firstImageFlipped = true;
Application.Current.Properties["FirstImageUrl"] = selecteditem.imageUrl;
Application.Current.Properties["FirstImageItem"] = selecteditem;
}
}
来源:https://stackoverflow.com/questions/59284414/xamarin-forms-different-item-is-invoking-instead-of-selected-item-in-flowlistvi