Silverlight图形像素侧面位置?

时间:2009-10-05 19:54:54

标签: c# silverlight graphics layout

我尝试将简单游戏移植到silverlight(SameGame)。问题是我的旧源代码使用了像素大小来登上游戏标记。我使用线条和游戏标记(使用矩形)绘制简单的网格。我怎样才能正确设置出租位置? 示例20左上角20像素。

        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);
        }            
    }

1 个答案:

答案 0 :(得分:0)

您需要将Canvas放在LayoutRoot Grid下面或将LayoutRoot更改为Canvas。 然后将recangle添加到Canvas.Children。