C# 比较图像源

C# 比较图像源,c#,wpf,xaml,C#,Wpf,Xaml,我有一个记忆游戏,在这个游戏中我必须检查两张牌是否相等。 我做了一个if语句,检查两个图像源是否相等,但即使它们相等,由于某种原因它也看不到。我尝试了“==”运算符和.Equal,但它们都不起作用。我有一个名为images的目录,在该文件夹中有8个图像,名称为1.png、2.png等等 这是我的代码: public class MemoryGrid { private Grid grid; private int rows, cols; public MemoryGri

我有一个记忆游戏,在这个游戏中我必须检查两张牌是否相等。 我做了一个if语句,检查两个图像源是否相等,但即使它们相等,由于某种原因它也看不到。我尝试了“==”运算符和.Equal,但它们都不起作用。我有一个名为images的目录,在该文件夹中有8个图像,名称为1.png、2.png等等

这是我的代码:

public class MemoryGrid
{
    private Grid grid;
    private int rows, cols;

    public MemoryGrid(Grid grid, int rows, int cols)
    {
        this.grid = grid;
        this.rows = rows;
        this.cols = cols;

        InitializeGrid();
        AddImages();
    }


    private void InitializeGrid()
    {
        for (int i = 0; i < rows; i++)
        {
            grid.RowDefinitions.Add(new RowDefinition());
        }
        for (int i = 0; i < cols; i++)
        {
            grid.ColumnDefinitions.Add(new ColumnDefinition());
        }
    }

    private void AddImages()
    {
        List<ImageSource> images = GetImagesList();
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                Image back = new Image();
                back.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));

                back.MouseDown += new System.Windows.Input.MouseButtonEventHandler(CardClick);

                back.Tag = images.First();
                images.RemoveAt(0);
                Grid.SetColumn(back, col);
                Grid.SetRow(back, row);
                grid.Children.Add(back);
            }
        }
    }



    static int numberOfClicks = 0;
    private Image card;
    static int score;
    private Image Image1;
    private Image Image2;



    private void CardClick(object sender, MouseButtonEventArgs e)
    {
        if (hasDelay) return;

        Image card = (Image)sender;
        ImageSource front = (ImageSource)card.Tag;
        card.Source = front;
        numberOfClicks++;

        checkCards(card);
    }

    private void checkCards(Image card)
    {

        this.card = card;
        if (numberOfClicks < 2 || numberOfClicks == 2)
        {

            if (this.Image1 == null)
            {
                Image1 = card;
            }
            else if (this.Image2 == null)
            {
                Image2 = card;
            }
        }

        if (numberOfClicks == 2)
        {
            checkPair(Image1, Image2);

            numberOfClicks = 0;
            Image1 = null;
            Image2 = null;
        }
    }

    public void checkPair(Image card1, Image card2)
    {
        this.Image1 = card1;
        this.Image2 = card2;

        if (card1.Source.Equals(card2.Source))
        {
            score++;       
        }
        else
        {
            resetCards(Image1, Image2);
        }

    }

    private bool hasDelay;
    private async void resetCards(Image card1, Image card2)
    {
        this.Image1 = card1;
        this.Image2 = card2;

        hasDelay = true;
        await Task.Delay(1000);


        card1.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));
        card2.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));
        hasDelay = false;
    }

    public List<ImageSource> GetImagesList()
    {
        List<ImageSource> images = new List<ImageSource>();
        List<string> random1 = new List<string>();
        List<string> random2 = new List<string>();

        for (int i = 0; i < 16; i++)
        {
            if (i < 8)
            {
                int imageNR = 0;

                Random rnd = new Random();
                imageNR = rnd.Next(1, 9);
                if (random1.Contains(Convert.ToString(imageNR)))
                {
                    i--;
                }
                else
                {
                    random1.Add(Convert.ToString(imageNR));
                    ImageSource source = new BitmapImage(new Uri("images/" + imageNR + ".png", UriKind.Relative));
                    images.Add(source);
                }
            }
            if (i >= 8)
            {
                int imageNR = 0;

                Random rnd = new Random();
                imageNR = rnd.Next(1, 9);
                if (random2.Contains(Convert.ToString(imageNR)))
                {
                    i--;
                }
                else
                {
                    random2.Add(Convert.ToString(imageNR));
                    ImageSource source = new BitmapImage(new Uri("images/" + imageNR + ".png", UriKind.Relative));
                    images.Add(source);
                }
            }
        }
        return images;
    }
}
公共类MemoryGrid
{
私有电网;
私有int行,cols;
公共内存网格(网格、整数行、整数列)
{
this.grid=grid;
this.rows=行;
this.cols=cols;
InitializeGrid();
AddImages();
}
私有void InitializeGrid()
{
对于(int i=0;i=8)
{
int-imageNR=0;
随机rnd=新随机();
imageNR=rnd.Next(1,9);
if(random2.Contains(Convert.ToString(imageNR)))
{
我--;
}
其他的
{
random2.Add(Convert.ToString(imageNR));
ImageSource source=新的位图图像(新Uri(“图像/”+imageNR+“.png”,UriKind.Relative));
图片。添加(源);
}
}
}
返回图像;
}
}

真的有必要向我们展示您的所有代码吗?