C#同时更新几个图片框

C# Update few Picture Boxes at the same time

我的应用程序中有 5 个图片框,我同时更新它们。 我希望他们显示 5 不同 图像,我的程序代码是正确的,但由于某种原因,图片框显示所有相同的图像...

代码如下:

private System.Drawing.Bitmap DiceOne;
private System.Drawing.Bitmap DiceTwo;
private System.Drawing.Bitmap DiceThree;
private System.Drawing.Bitmap DiceFour;
private System.Drawing.Bitmap DiceFive;
private System.Drawing.Bitmap DiceSix;

public void Prepare()
{
    for (int i = 0; i < 5; i++)
        Dices[i] = new Dice();

    DiceOne = Properties.Resources.Würfel_1;
    DiceTwo = Properties.Resources.Würfel_2;
    DiceThree = Properties.Resources.Würfel_3;
    DiceFour = Properties.Resources.Würfel_4;
    DiceFive = Properties.Resources.Würfel_5;
    DiceSix = Properties.Resources.Würfel_6;
}

public void Roll()
{
    foreach (Dice dice in Dices)
        dice.RollTheDice();

    View.SuspendLayout();
    View.diceOnePictureBox.Image = DiceNumberToBmp(Dices[0].GetLastRolled());
    View.diceOnePictureBox.Update();
    View.diceTwoPictureBox.Image = DiceNumberToBmp(Dices[1].GetLastRolled());
    View.diceOnePictureBox.Update();
    View.diceThreePictureBox.Image = DiceNumberToBmp(Dices[2].GetLastRolled());
    View.diceOnePictureBox.Update();
    View.diceFourPictureBox.Image = DiceNumberToBmp(Dices[3].GetLastRolled());
    View.diceOnePictureBox.Update();
    View.diceFivePictureBox.Image = DiceNumberToBmp(Dices[4].GetLastRolled());
    View.diceOnePictureBox.Update();
    View.ResumeLayout(false);
}

private System.Drawing.Bitmap DiceNumberToBmp(int number)
{
    switch(number)
    {
        case 1:
            return DiceOne;
        case 2:
            return DiceTwo;
        case 3:
            return DiceThree;
        case 4:
            return DiceFour;
        case 5:
            return DiceFive;
        case 6:
            return DiceSix;
        default:
            return null;
    }
}

我之前在互联网上阅读过一些比较熟悉的帖子,并尝试使用 SuspendLayout、ResumeLayout、更新 PictureBox 来解决......对我来说没有任何效果。

我的骰子class:

using System;

namespace KniffelGUI.Model
{
    public class Dice
    {

        private int LastRolled;

        public Dice()
        {
            RollTheDice();
        }

        public Dice(int fakeRoll)
        {
            LastRolled = fakeRoll;
        }

        public int RollTheDice()
        {
            LastRolled = new Random().Next(6) + 1;

            return LastRolled;
        }

        public int GetLastRolled()
        {
            return LastRolled;
        }

    }
}

问题出在您的 Dice class 上,正如 Barry O'Kane 之前所建议的那样。

改为:

public class Dice
{

    private int LastRolled;
    private static Random R = new Random();

    public Dice()
    {
        RollTheDice();
    }

    public Dice(int fakeRoll)
    {
        LastRolled = fakeRoll;
    }

    public int RollTheDice()
    {
        LastRolled = R.Next(6) + 1;
        return LastRolled;
    }

    public int GetLastRolled()
    {
        return LastRolled;
    }

}

您的症状是由于这条线引起的:\

LastRolled = new Random().Next(6) + 1;

当您使用默认构造函数创建 Random 实例时,它使用当前时间作为种子。当您快速连续创建多个实例时,它们最终会得到相同的种子,因此不会得到不同的数字。

通过将其声明为静态,class 的所有实例都将使用相同的 Random 实例并在每次调用时获得不同的数字。