将背景分配给按钮控件时出现问题 [英] Problem when assigning Background to Button control

查看:115
本文介绍了将背景分配给按钮控件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows Phone 7应用程序.

迭代执行按钮时,为按钮分配新背景无法正常工作.

我有一种在重置游戏时会调用的方法.所有按钮的背景都更改为新的.但这不能正常工作.有时,背景未分配给新背景,而旧背景仍然保留.有人可以解决这个问题吗?

I am working on a windows phone 7 application.

Assigning a new background to a button does not work properly when it is executed iteratively.

I have a method which is called when the game is reset. All the buttons'' backgrounds are changed to new ones. But this doesnt work properly. Sometimes, the backgrounds are not assigned to new ones, and old ones remain. Can someone help with this problem.

public void PaintButtons()
        {
            p1numlist = GenerateRandom();
            p2numlist = GenerateRandom();
            
                
                p1b1.Background = new SolidColorBrush(a[(int)p1numlist[0]]);
                p1b2.Background = new SolidColorBrush(a[(int)p1numlist[1]]); 
                p1b3.Background = new SolidColorBrush(a[(int)p1numlist[2]]);
                p2b1.Background = new SolidColorBrush(a[(int)p2numlist[0]]);          
                p2b2.Background = new SolidColorBrush(a[(int)p2numlist[1]]);          
                p2b3.Background = new SolidColorBrush(a[(int)p2numlist[2]]);
                
                imageindex = rand.Next(3);
                image1.Source = new BitmapImage(uri[imageindex]);
            
            
        }

推荐答案

您确定该错误与绘制按钮有关吗?

看起来您正在生成1到3之间的随机数,然后将其用作颜色数组的索引.您有33%的机会每次都会变黄.与红色和蓝色相同.因此,偶尔看到这些按钮具有相同的颜色也就不足为奇了.

换句话说,颜色在变化,当它们从黄色变为黄色时,您根本看不到.

那有什么不同的方法呢?您可能会循环使用一系列画笔.您可以选择随机偏移量并使用%,而不是随机数.模运算符 [ ^ ]以帮助获取新"画笔颜色.

抱歉,如果我没有正确阅读您的代码...如果情况并非如此,也许将调试器附加到您的代码上,设置一些断点,甚至进行一些日志记录,以查看旧的颜色和新的颜色颜色分别是.

希望对您有所帮助.

干杯.
Are you sure that the bug is with the buttons drawing?

Looks like you''re generating a random number between 1 and 3, then using that as an index into an array of colors. You have a 33% chance of getting yellow every time. Same with red and blue. So it''s not surprising to see that the buttons occasionally get the same colors.

Said another way...the colors are changing, you just don''t see when they change from Yellow to Yellow.

What about a different approach? You could have an array of brushes that you just cycle through. Rather than picking random numbers, you could pick a random offset and use %, the modulus operator[^] to help grab the ''new'' brush color.

Sorry if I''m not reading your code correctly...if this doesn''t appear to be the case, perhaps attach a debugger to your code, set some break points and even some logging to see what the old color and new color were and are.

Hope this helps some.

Cheers.


请参阅我对上一个问题的解决方案(

See my solution to your previous question (Problem when loading images to buttons dynamically in Windows Phone application[^]). It looks like the same problem.


问题已解决.使用背景参考实际上可以解决问题,而不是直接更改按钮的背景.

我使用以下代码分别更改了每个按钮的背景
Probem is solved. Instead of directly changing the background of the button, using the reference of the background actually solves the problem.

I used the following code to change the backround for each button individually
public void PaintButtons()
        {
            
            p1list = GenerateRandom();
            p2list = GenerateRandom();


            ChangeBackground(p1b1, p1b2, p1b3, p2b1, p2b2, p2b3);
            ***********
            **********
        }
public void ChangeBackground(Button one, Button two, Button three, Button four, Button five, Button six)
        {
            SolidColorBrush s = (SolidColorBrush)one.Background;
            s.Color = a[(int)p1list[0]];
            one.Background = s;
             s = (SolidColorBrush)two.Background;
            s.Color = a[(int)p1list[1]];
            two.Background = s;
             s = (SolidColorBrush)three.Background;
            s.Color = a[(int)p1list[2]];
            three.Background = s;
             s = (SolidColorBrush)four.Background;
            s.Color = a[(int)p2list[0]];
            four.Background = s;
             s = (SolidColorBrush)five.Background;
            s.Color = a[(int)p2list[1]];
            five.Background = s;
             s = (SolidColorBrush)six.Background;
            s.Color = a[(int)p2list[2]];
            six.Background = s;
        }


这篇关于将背景分配给按钮控件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆