none
Performance tips for Metro style XAML apps

Všechny reakce

  • Helpful info! Many thanks!

    We create delicious software http://verysoftware.com

    24. října 2012 18:22
  • using System.Collections;
    using System.Windows.Threading;

    namespace RandomExample
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        /// 

        public partial class MainWindow : Window
        {
            private ArrayList teamList = new ArrayList();
            
            private ArrayList usedNumber = new ArrayList(); // could just use the TeamList arrlaylist and check there

            private int teamCount = 0;

            private DispatcherTimer timer;

            public MainWindow()
            {
                InitializeComponent();

                timer = new DispatcherTimer();
                
                timer.Interval = TimeSpan.FromMilliseconds(1000);
                
                timer.Tick += new EventHandler(timer_Tick); // wiring tick event to a handler
                
                timer.Start();

                // to stop the timer at any time use timer.Stop;
                
            }
            
            private void timer_Tick(object sender, EventArgs e)
            {
                // what ever code you put in here will be executed every 1000 milliseconds

                this.Title = DateTime.Now.ToLongTimeString();
            }
            
            
            private bool Check(int randomIndex)
            // method to check if number used already
            {
                bool used = false;
                
                if (usedNumber.Contains(randomIndex))
                            used = true;
                
                return used;
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                string[] TeamNames = { "reds", "blacks", "blues", "yellows", "greens" };

                Random random = new Random(DateTime.Now.Millisecond);

                bool used;

                int randomIndex = random.Next(0, 5); // generate a random score between 0 and 4 
                
                used = Check(randomIndex);
                while ((used == true) && teamCount < 5)
                {
                    randomIndex = random.Next(0, 5);
                    used = Check(randomIndex);
                }
                usedNumber.Add(randomIndex);

                
                string randomName = TeamNames[randomIndex]; // pull out the team

                int randomScore =  random.Next(0, 21); // generate a random score between 0 and 20 
                
                if (teamCount < 5) // check that only 5 teams
                {
                    Team aTeam = new Team();
                    aTeam.TeamName = randomName;
                    aTeam.Score = randomScore;

                    teamCount++;
                    listBox1.Items.Add(aTeam);
                    teamList.Add(aTeam);
                }      
    8. listopadu 2012 23:38