1

我首先在我的程序上创建了大量按钮,从 1 到 96。计划是当一个按钮被按下时,它会变成红色,这会随机地抓住附近的其他按钮。

我遇到的问题是按钮不希望变成红色。下面,我将展示我尝试这样做的代码:

public MainPage()
        {

            InitializeComponent();
            numbers = new[] {
            button1, button2, button3, button4, button5, button6, button7, button8, button9, button10,
            button11, button12, button13, button14, button15, button16, button17, button18, button19,
            button20, button21, button22, button23, button24, button25, button26, button27, button28,
            button29, button30, button31, button32, button33, button34, button35, button36, button37,
            button38, button39, button40, button41, button42, button43, button44, button45, button46,
            button47, button48, button49, button50, button51, button52, button53, button54, button55,
            button56, button57, button58, button59, button60, button61, button62, button63, button64,
            button65, button66, button67, button68, button69, button70, button71, button72, button73,
            button74, button75, button76, button77, button78, button79, button80, button81, button82,
            button83, button84, button85, button86, button87, button88, button89, button90, button91,
            button92, button93, button94, button95, button96
            };

        }

        private void ButtonSequence(int bRef)
        {
            Random random = new Random();
            int activeCell = bRef;
            int randomNumber = random.Next(0, 10);
            int steps = 0;
            int randomMaxRange = 2;

            SolidColorBrush red = new SolidColorBrush(Colors.Red);

            numbers[activeCell].Background = red;

                if (activeCell == 0)
                {
                    randomNumber = random.Next(0, 10);
                    if (randomNumber < randomMaxRange)
                    {
                        steps++;
                        ButtonSequence(1);
                    }

此函数继续,并由以下方式调用:

private void button1_Click(object sender, RoutedEventArgs e)
{
    ButtonSequence(0);
}

和别的。

有没有人解决这个问题?谢谢。

4

1 回答 1

0

如果您需要在应用程序中手动创建 96 个按钮,则说明您处理数据模型的方式有问题。此外,在您的代码中,您实际上从未将按钮添加到页面可视树中。您的页面某处是否有数据容器?

您似乎也没有将事件处理程序绑定到 UI 控件。

于 2012-07-02T19:34:03.043 回答