【问题标题】:Issue Comparing items from two System.Drawing.Image Collections to see if they match问题比较两个 System.Drawing.Image 集合中的项目以查看它们是否匹配
【发布时间】:2012-12-19 22:56:58
【问题描述】:

我正在 WPF 中制作具有拖放功能的图片益智游戏。上传图像时,它会被切割成 9 块,每个位图都添加到 System.Drawing.Images 的集合(collection1)中。我比较的是这个集合,看看重新排列时的图像是否正确。然后,我将每个位图随机分配到网格上的一个位置,并放入 System.Drawing.Images 的另一个集合(collection2)中。

当我将图像拖放到网格上时,我会更改图像在集合中的位置2。 在网格上移动图像后,我检查(验证)以查看每个集合中的图像位置(索引)是否匹配。如果是这样,图片已重新排列。希望我的解释正确。

我的问题是该方法总是返回 false,即使图像已被安排完成图像。

如果我需要提供更多代码或更好地解释,请告诉我。我想把这个说清楚才能明白,但我的头是炸的

    public bool Validate(ObservableCollection<System.Drawing.Image> itemPlacement)
    {
        ObservableCollection<System.Drawing.Image> placement = itemPlacement;

        foreach (System.Drawing.Image item in placement)
        {
            if ((placement.IndexOf(item) != puzzlePiece.IndexOf(item) || placement.IndexOf(item) < 0))
            {
                return false;
            }
        }

        return true;
    }

【问题讨论】:

  • 你能贴出用新索引更新collection2的代码吗?

标签: c# wpf image observablecollection


【解决方案1】:

据我了解,您有一个集合以特定顺序存储一组已知图像,并且您有另一个集合(来自第一个集合的相同图像)代表用户放置它们的顺序并且您想要确定如果订单匹配。如果他们这样做了,用户就成功地解决了这个难题。

嗯...很简单 - 创建一个带有索引的类并进行比较。

public class PuzzlePiece
{
  public int Index { get; set; }
  public Image Piece { get; set; }
}

for(int i = 0; i < puzzlePieces.Count; i++)
{
  if(puzzlePieces[i].Index != playerPieces[i].Index) return false;
}
return true;

此外,您可以实现 IComparable 并根据您的索引进行排序 - 但在按顺序添加图像时,在小型集合中可能不需要。但是,IComparable 允许您说:

if(puzzlePieces[i].CompareTo(playerPieces[i]) != 0) return false;

【讨论】:

    猜你喜欢
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2019-11-04
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多