在Windows Phone应用程序中将图像动态加载到按钮时出现问题 [英] Problem when loading images to buttons dynamically in Windows Phone application

查看:111
本文介绍了在Windows Phone应用程序中将图像动态加载到按钮时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Windows Phone 7应用程序.我有两组,每组三个按钮p1b1,p1b2,p1b3 & p2b1,p2b2,p2b3,正在将图像blue.png, red.png, yellow.png随机加载到按钮上.组中的每个按钮应具有不同的颜色(即p1b1,p1b2,p1b3应具有三种不同的颜色).我以下面的方式编写了代码,但是当我调用ResetForNextRound()重新绘制按钮时,某些按钮不会更改其背景.可能是什么问题.我是否没有以正确的方式存储图像?请提出建议.

 公共 部分  class  GamePage:PhoneApplicationPage
    {
        IList< ImageBrush>图片;
        IList< ImageBrush> images2;
        Random rand =  Random(DateTime.Now.Millisecond);
        Uri [] uri =  Uri [ 3 ];
        IList p1numlist;
        IList p2numlist;
         int  imageindex;
         int  p1score;
         int  p2score;
         int  roundnumber =  1 ;
        公共 GamePage()
        {
            InitializeComponent();
            LoadImages();
            image2.Visibility = Visibility.Collapsed;
            image3.Visibility = Visibility.Collaped;
            PaintButtons();
        }

        公共 无效 LoadImages()
        {

            图片= 新建列表< ImageBrush>();
            images2 = 列表< ImageBrush>();
            uri [ 0 ] =  Uri(" /BtnPics/Blue.png",UriKind.Relative);
            uri [ 1 ] =  Uri(" /BtnPics/Red.png",UriKind.Relative);
            uri [ 2 ] =  Uri(" /BtnPics/Yellow.png",UriKind.Relative);
            
             foreach (在 uri中,
            {
                BitmapImage bmp =  BitmapImage(u);
                images.Add( ImageBrush(){ImageSource = bmp});
                images2.Add( ImageBrush(){ImageSource = bmp});
            }
        }
        
        公共 无效 PaintButtons()
        {
// 这是我为按钮分配背景的地方.我认为这部分应该考虑.
// 按顺序分配背景时,其中一些背景无法分配并保留以前的值.我不知道为什么会这样.
            p1numlist = GenerateRandom();
            p2numlist = GenerateRandom();

            p1b1.Background =图片[( int )p1numlist [ 0 ]];
            p1b2.Background =图片[( int )p1numlist [ 1 ]];
            p1b3.Background =图片[( int )p1numlist [ 2 ]];
            p2b1.Background = images2 [( int )p2numlist [ 0 ]];
            p2b2.Background = images2 [( int )p2numlist [ 1 ]];
            p2b3.Background = images2 [( int )p2numlist [ 2 ]];
            imageindex = rand.Next( 3 );
            image1.Source =  BitmapImage(uri [imageindex]);

            
        }
        公共列表< int> GenerateRandom()
        {
            
            List< int> numlist =  List< int>();
            numlist.Add(rand.Next( 3 ));
             int  count =  0 ;
            
            {
                 int  num = rand.Next( 3 );
                如果(!numlist.Contains(num))
                {
                    numlist.Add(num);
                    数++;
                }

            } 同时(计数<   2 )) ;
            返回数字列表;
        }
        布尔 PlayerOne =  false ;
        布尔 PlayerTwo =  false ;
         bool  SomeoneScored =  false ;
        公共 无效 ProcessPress(对象发​​送者)
        {
            按钮b =发件人作为按钮;
            如果(b.Name.StartsWith(" ))
            {
                PlayerOne =  true ;
                DisableButtonsExceptPressed(sender);
            }
            如果(b.Name.StartsWith(" ))
            {
                PlayerTwo =  true ;
                DisableButtonsExceptPressed(sender);
            }
            MatchImage(sender);
        }
        公共 无效 DisableButtonsExceptPressed(对象发​​件人)
        {
            按钮b =发件人作为按钮;

            如果(b.Name.StartsWith(" ))
            {
                p1b1.IsEnabled =  false ;
                p1b2.IsEnabled =  false ;
                p1b3.IsEnabled =  false ;
                b.IsEnabled =  true ;
            }
            如果(b.Name.StartsWith(" ))
            {
                p2b1.IsEnabled =  false ;
                p2b2.IsEnabled =  false ;
                p2b3.IsEnabled =  false ;
                b.IsEnabled =  true ;
            }
            
        }

        公共 无效 MatchImage(对象发​​送者)
        {
            
            按钮b =发件人作为按钮;
             int 按钮编号=( int )(b.Name [ 3 ]);
            
            如果(b.Name.StartsWith(" ))
                如果(imageindex ==( int )p1numlist [buttonnumber-49])
                {
                        p1score ++; // 与玩家一匹配
                        image2.Visibility = Visibility.Visible;
                        SomeoneScored =  true ;
                        DisableAllButtons(); // 禁用所有按钮
                }
            如果(b.Name.StartsWith(" ))
                如果(imageindex ==( int )p2numlist [buttonnumber-49])
                {
                        p2score ++; // 与第二个玩家匹配
                        image3.Visibility = Visibility.Visible;
                        SomeoneScored =  true ;
                        DisableAllButtons(); // 禁用所有按钮
                }
            如果(((PlayerOne == && PlayerTwo ==  true )
            {
                四舍五入++;
                ResetForNextRound();
                
            }
            
        }
        公共 无效 DisableAllButtons()
        {
            p1b1.IsEnabled =  false ;
            p1b2.IsEnabled =  false ;
            p1b3.IsEnabled =  false ;
            p2b1.IsEnabled =  false ;
            p2b2.IsEnabled =  false ;
            p2b3.IsEnabled =  false ;
            //  DisplayWhoScored(sender); 
        }
公共 无效 ResetForNextRound()
        {
            如果(轮数<  =  10 )
            {
                //  SetNullBackground(); 
                EnableAllButtons();
                LoadImages();
                PaintButtons();
                p1Scorelabel.Text = p1score.ToString();
                p2Scorelabel.Text = p2score.ToString();
                p1Roundlabel.Text = roundnumber.ToString();
                p2Roundlabel.Text = roundnumber.ToString();
                image2.Visibility = Visibility.Collapsed;
                image3.Visibility = Visibility.Collaped;
                PlayerOne =  false ;
                PlayerTwo =  false ;
                SomeoneScored =  false ;
            }
           
                
        }
                
        公共 无效 EnableAllButtons()
        {
            p1b1.IsEnabled =  true ;
            p1b2.IsEnabled =  true ;
            p1b3.IsEnabled =  true ;
            p2b1.IsEnabled =  true ;
            p2b2.IsEnabled =  true ;
            p2b3.IsEnabled =  true ;
        }

// 忽略以下部分.
        私有 无效 p1b1_ManipulationStarted(对象发​​件人,ManipulationStartedEventArgs e)
        {
            ProcessPress(发送者);
        }

        私有 无效 p1b2_ManipulationStarted(对象发​​件人,ManipulationStartedEventArgs e)
        {
            ProcessPress(发送者);
        }

        私有 无效 p1b3_ManipulationStarted(对象发​​件人,ManipulationStartedEventArgs e)
        {
            ProcessPress(发送者);
        }

        私有 无效 p2b1_ManipulationStarted(对象发​​件人,ManipulationStartedEventArgs e)
        {
            ProcessPress(发送者);
        }

        私有 无效 p2b2_ManipulationStarted(对象发​​件人,ManipulationStartedEventArgs e)
        {
            ProcessPress(发送者);
        }

        私有 无效 p2b3_ManipulationStarted(对象发​​件人,ManipulationStartedEventArgs e)
        {
            ProcessPress(发送者);
        }

    } </  int  >  </  int  >  </  int  >  



也许我在这里转储了很多代码,但是我不确定哪一部分真正导致了问题.很抱歉造成麻烦.

解决方案

您确定按钮的背景没有更改吗?也许它们已更改,但更改为具有相同图像的ImageBrush.


由于使用Random来生成图像的索引,因此按钮的索引可能相同.尝试存储每个按钮的当前索引,并检查新索引是否与旧索引不同.


问题已在我的另一篇文章中解决. 将背景分配给Button控件时出现的问题 [ ^ ]

I am building a windows phone 7 application. I have two groups of three buttons p1b1,p1b2,p1b3 & p2b1,p2b2,p2b3 I am loading the images blue.png, red.png, yellow.png on a random basis to the buttons. Each button in a group should have different colors (i.e p1b1,p1b2,p1b3 should have three different colors). I have written the code in the following way, but when i call the ResetForNextRound() to repaint the buttons, some buttons do not change their background. What can be the problem. Am I not storing the Images in a proper way? Please Suggest.

 public partial class GamePage : PhoneApplicationPage
    {
        IList<ImageBrush> images;
        IList<ImageBrush> images2;
        Random rand = new Random(DateTime.Now.Millisecond);
        Uri[] uri = new Uri[3];
        IList p1numlist;
        IList p2numlist;
        int imageindex;
        int p1score;
        int p2score;
        int roundnumber=1;
        public GamePage()
        {
            InitializeComponent();
            LoadImages();
            image2.Visibility = Visibility.Collapsed;
            image3.Visibility = Visibility.Collapsed;
            PaintButtons();
        }

        public void LoadImages()
        {

            images = new List<ImageBrush>();
            images2 = new List<ImageBrush>();
            uri[0] = new Uri("/BtnPics/Blue.png", UriKind.Relative);
            uri[1] = new Uri("/BtnPics/Red.png", UriKind.Relative);
            uri[2] = new Uri("/BtnPics/Yellow.png", UriKind.Relative);
            
            foreach (Uri u in uri)
            {
                BitmapImage bmp = new BitmapImage(u);
                images.Add(new ImageBrush() { ImageSource = bmp });
                images2.Add(new ImageBrush() { ImageSource = bmp });
            }
        }
        
        public void PaintButtons()
        {
// This is where I am assigning the Background for the buttons. I think this part should be considered.
//When the Backgrounds are sequentially assigned, some of them fail to assign and remain with previous values. I have no idea why this is happening.
            p1numlist = GenerateRandom();
            p2numlist = GenerateRandom();

            p1b1.Background = images[(int)p1numlist[0]];
            p1b2.Background = images[(int)p1numlist[1]]; 
            p1b3.Background = images[(int)p1numlist[2]];
            p2b1.Background = images2[(int)p2numlist[0]];
            p2b2.Background = images2[(int)p2numlist[1]];
            p2b3.Background = images2[(int)p2numlist[2]];
            imageindex = rand.Next(3);
            image1.Source = new BitmapImage(uri[imageindex]);

            
        }
        public List<int> GenerateRandom()
        {
            
            List<int> numlist = new List<int>();
            numlist.Add(rand.Next(3));
            int count = 0;
            do
            {
                int num = rand.Next(3);
                if (!numlist.Contains(num))
                {
                    numlist.Add(num);
                    count++;
                }

            } while (count < 2);
            return numlist;
        }
        bool PlayerOne = false;
        bool PlayerTwo = false;
        bool SomeoneScored=false;
        public void ProcessPress(object sender)
        {
            Button b = sender as Button;
            if (b.Name.StartsWith("p1"))
            {
                PlayerOne = true;
                DisableButtonsExceptPressed(sender);
            }
            if(b.Name.StartsWith("p2"))
            { 
                PlayerTwo = true;
                DisableButtonsExceptPressed(sender);
            }
            MatchImage(sender);
        }
        public void DisableButtonsExceptPressed(object sender)
        {
            Button b = sender as Button;

            if (b.Name.StartsWith("p1"))
            {
                p1b1.IsEnabled = false;
                p1b2.IsEnabled = false;
                p1b3.IsEnabled = false;
                b.IsEnabled = true;
            }
            if(b.Name.StartsWith("p2"))
            {
                p2b1.IsEnabled = false;
                p2b2.IsEnabled = false;
                p2b3.IsEnabled = false;
                b.IsEnabled = true;
            }
            
        }

        public void MatchImage(object sender)
        {
            
            Button b = sender as Button;
            int buttonnumber = (int)(b.Name[3]);
            
            if (b.Name.StartsWith("p1"))
                if (imageindex == (int)p1numlist[buttonnumber-49])
                {
                        p1score++;//matched for player one
                        image2.Visibility = Visibility.Visible;
                        SomeoneScored = true;
                        DisableAllButtons(); //disable all buttons
                }
            if(b.Name.StartsWith("p2"))
                if (imageindex == (int)p2numlist[buttonnumber-49])
                {
                        p2score++;//matched for player two
                        image3.Visibility = Visibility.Visible;
                        SomeoneScored = true;
                        DisableAllButtons(); //disable all buttons
                }
            if ((PlayerOne == true && PlayerTwo == true)||SomeoneScored==true)
            {
                roundnumber++;
                ResetForNextRound();
                
            }
            
        }
        public void DisableAllButtons()
        {
            p1b1.IsEnabled = false;
            p1b2.IsEnabled = false;
            p1b3.IsEnabled = false;
            p2b1.IsEnabled = false;
            p2b2.IsEnabled = false;
            p2b3.IsEnabled = false;
            //DisplayWhoScored(sender);
        }
public void ResetForNextRound()
        {
            if (roundnumber <= 10)
            {
                //SetNullBackground();
                EnableAllButtons();
                LoadImages();
                PaintButtons();
                p1Scorelabel.Text = p1score.ToString();
                p2Scorelabel.Text = p2score.ToString();
                p1Roundlabel.Text = roundnumber.ToString();
                p2Roundlabel.Text = roundnumber.ToString() ;
                image2.Visibility = Visibility.Collapsed;
                image3.Visibility = Visibility.Collapsed;
                PlayerOne = false;
                PlayerTwo = false;
                SomeoneScored = false;
            }
           
                
        }
                
        public void EnableAllButtons()
        {
            p1b1.IsEnabled = true;
            p1b2.IsEnabled = true;
            p1b3.IsEnabled = true;
            p2b1.IsEnabled = true;
            p2b2.IsEnabled = true;
            p2b3.IsEnabled = true;
        }

//ignore the following part.
        private void p1b1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p1b2_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p1b3_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p2b1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p2b2_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p2b3_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

    }</int></int></int>



Maybe I am dumping a whole lot of code here, but I wasn''t sure which part is actually causing the problem. Sorry for the trouble.

解决方案

Are you sure that the buttons'' backgrounds aren''t changed? Maybe they are changed but, to ImageBrushes with the same image.


Since you use Random for generating the images'' indices, maybe you got the same indices for the buttons. Try to store the current index for each button and, check that the new indices are differnet from the old indices.


Problem solved in my other post. Problem when assigning Background to Button control[^]


这篇关于在Windows Phone应用程序中将图像动态加载到按钮时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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