有效的方式改变颜色与定时器块

本文关键字:定时器 变颜色 方式改 有效 | 更新日期: 2023-09-27 18:19:24

我是新的编程,我一直在c#编程在过去的3个月,现在我试图做一个应用程序,在WP8.1上,它包括创建一个屏幕与4块,分配一个计时器,并改变4块的颜色作为计时器"滴答"。

目标是方块的颜色不重复(例如,如果第一个方块是红色的,那么它就不可能是蓝色或绿色…)

我已经达到了我的目标,但是我觉得我的代码有点马虎,这就是为什么我在这里…

public void ColorTimerChange(object sender, object args)
{
    count++;
    //Sets the text of a TextBlock so that I can check the time
    TimerTB.Text = count.ToString();
    //Creates a new Random instance
    Random rand = new Random();

    if (count % count == 0)
    {
        //Generates random numbers between 1 and 4 for every color.. If there are numbers that are equal, generate again
        RandBlue = rand.Next(1, 5);
        RandGreen = rand.Next(1, 5);
        RandRed = rand.Next(1, 5);
        while (RandBlue == RandGreen || RandBlue == RandRed || RandGreen == RandRed)
        {
            RandBlue = rand.Next(1, 5);
            RandGreen = rand.Next(1, 5);
            RandRed = rand.Next(1, 5);
        }
        //Textblocks so that I can check the random numbers 
        RedTB.Text = RandRed.ToString();
        RandomTB.Text = RandBlue.ToString();
        GreenTb.Text = RandGreen.ToString();

            switch (RandRed)
            {
               //if the random number for red is equal to 1, change the color of the 1st block to red. If the others random numbers are different to 2, change the color of the 2nd block to white, and so on.
                case(1):
                    {
                        Block1.Fill = color.Red;
                        if (RandGreen != 2 && RandBlue != 2)
                            Block2.Fill = color.White;
                        if (RandGreen != 3 && RandBlue != 3)
                            Block3.Fill = color.White;
                        if (RandGreen != 4 && RandBlue != 4)
                            Block4.Fill = color.White;
                        break;
                    }
                case (2):
                    {
                        Block2.Fill = color.Red;
                        if (RandGreen != 1 && RandBlue != 1)
                            Block1.Fill = color.White;
                        if (RandGreen != 3 && RandBlue != 3)
                            Block3.Fill = color.White;
                        if (RandGreen != 4 && RandBlue != 4)
                            Block4.Fill = color.White;
                        break;
                    }
                case (3):
                    {
                        Block3.Fill = color.Red;
                        if (RandGreen != 2 && RandBlue != 2)
                            Block2.Fill = color.White;
                        if (RandGreen != 1 && RandBlue != 1)
                            Block1.Fill = color.White;
                        if (RandGreen != 4 && RandBlue != 4)
                            Block4.Fill = color.White;
                        break;
                    }
                case (4):
                    {
                        Block4.Fill = color.Red;
                        if (RandGreen != 2 && RandBlue != 2)
                            Block2.Fill = color.White;
                        if (RandGreen != 3 && RandBlue != 3)
                            Block3.Fill = color.White;
                        if (RandGreen != 1 && RandBlue != 1)
                            Block1.Fill = color.White;
                        break;
                    }
                default:
                    break;
            }
            ...
}

这是红色的例子。我对绿色和蓝色做了同样的转换。颜色是在"Helperz.cs"中定义的,我为代码中的每种颜色创建了新的SolidBrushColor(colors .color)。

我如何做同样的事情,但不需要"复制"粘贴,并对每个颜色/块重复做同样的事情。

提前感谢大家!

有效的方式改变颜色与定时器块

将您的Block保持在数组中。然后你可以简单地说:

Blocks[RandRed] = Color.Red;