Tuesday, March 27, 2012

WP7: Canvas, Random, 2D Array & Timer



// create a rectangle and fill it with color
Rectangle r = new Rectangle();
 r.Width = r.Height = 50;
r.Fill = new SolidColorBrush(Colors.Orange);
 r.Stroke = new SolidColorBrush(Colors.Black);
 r.StrokeThickness = 3;
// add to canvas
canvas1.Children.Add(r);
// set position

Canvas.SetLeft(r, x);
 Canvas.SetTop(r, y);


// random generator
Random rand = new Random();
int n = rand.Next(maxNumber); // integer :0 to less than maxNumber


// 2D Array
int[,] containers = new int[w, h];

for (int i = 0; i < w; i++ )
 {
       for (int k = 0; k < h; k++)
      {
           containers[i, k] = -1;
       }
 }


// Timer
using System.Windows.Threading;
DispatcherTimer timer = new DispatcherTimer();

timer.Interval = TimeSpan.FromSeconds(5); 
timer.Tick += new EventHandler(timer_Tick);


void timer_Tick(object sender, EventArgs e)
{
     // do something 
 }

No comments: