views:

60

answers:

1

I try to port simple game to silverlight (SameGame). The problem is that my old source code used pixel sizes to allight game marks to board. I draw simple grid using lines and game mark (using rectangle). How i can set rentacle position correctly? Example 20 20 pixels to upper left corner).

        private void DrawGrid()
    {
        LayoutRoot.Children.Clear();

        Rectangle r = new Rectangle();
        r.Width = 20;
        r.Height = 20;
        r.Fill = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
        r.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
        r.SetValue(Canvas.LeftProperty, (double)0);
        r.SetValue(Canvas.TopProperty, (double)0);                       
        LayoutRoot.Children.Add(r);


        Color GridColor = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);

        for (int y = 0; y < 11; y++)
        {
            Line l = new Line();
            l.X1 = 0;
            l.Y1 = 30 * y - 1;
            l.X2 = 20 * 30;
            l.Y2 = 30 * y - 1;
            l.Stroke = new SolidColorBrush(GridColor);
            l.StrokeThickness = 1;

            LayoutRoot.Children.Add(l);
        }

        for (int x = 0; x < 21; x++)
        {
            Line l = new Line();
            l.X1 = 30 * x;
            l.Y1 = 0;
            l.X2 = 30 * x;
            l.Y2 = 10 * 30;
            l.Stroke = new SolidColorBrush(GridColor);
            l.StrokeThickness = 1;

            LayoutRoot.Children.Add(l);
        }            
    }
A: 

You need to put a Canvas below the LayoutRoot Grid or change LayoutRoot to a Canvas. Then add the recangle to the Canvas.Children.

Michael S. Scherotter