简体   繁体   English

Windows Phone:如何在矩形上画一条线?

[英]Windows Phone: How to draw a line on a Rectangle?

I draw a rectangle and I need to divide this rectangle into 2 parts. 我绘制了一个矩形,我需要将此矩形分为2个部分。 I am trying to use line in order to divide it but I dont know why I can not see the line. 我正在尝试使用线来划分线,但我不知道为什么我看不到线。

Rectangle rect = new Rectangle();
rect.Fill= Colors.Blue;
rect.Width=100;
rect.Margin = new Thikness (0,40,0,0);
grid.Children.Add(rect);

Line line = new Line();
line.Stroke = Colors.Black;
line.StrokeThickness=1;
line.X1=2; 
line.X2=7;
line.Y1=41;
line.Y2=41;
 grid.Children.Add(line);

Do you have any idea that how can I add a line on the rectangle? 您是否知道如何在矩形上添加一条线?

Here is how you would divide it vertically:- 这是您将其垂直分割的方法:-

a) you were drawing a line 5 pixels in width but it was not visible because your line was of stroke Black and the page background was also Black so the line was hidden. a)您正在画一条宽度为5像素的线,但是它不可见,因为您的线是黑色笔触,并且页面背景也是黑色,因此该线是隐藏的。 The line was drawn from (2,41) to (7,41). 线从(2,41)绘制到(7,41)。

b) your rectangle was in the center of the page, 100 pixels wide and as tall as the page and thus the line which was on the top left did not intersect with the rectangle which was in the center b)您的矩形位于页面的中央,宽度为100像素,与页面一样高,因此位于左上方的线与位于中心的矩形不相交

c) I would recommend the use of a Canvas since you can easily set Top and Left pixel positions of each item on the Canvas c)我建议您使用Canvas,因为您可以轻松设置Canvas上每个项目的顶部和左侧像素位置

I modified your code in the following way:- 我通过以下方式修改了您的代码:-

Rectangle rect = new Rectangle();
rect.Fill = new SolidColorBrush(Colors.Blue);
rect.Width = 100;
rect.Height = 200;
Canvas.SetLeft(rect, 200);
Canvas.SetTop(rect, 20);
LayoutRoot.Children.Add(rect);

Line line = new Line();
line.Stroke = new SolidColorBrush(Colors.White);
line.StrokeThickness = 1;

line.X1 = Canvas.GetLeft(rect) + rect.Width / 2;
line.X2 = Canvas.GetLeft(rect) + rect.Width / 2;

line.Y1 = Canvas.GetTop(rect);
line.Y2 = Canvas.GetTop(rect) + rect.Height;

LayoutRoot.Children.Add(line);

1) LayoutRoot is my Canvas in the XAML...I have just changed it from Grid to Canvas in the default page template. 1)LayoutRoot是我在XAML中的Canvas ...我刚刚在默认页面模板中将它从Grid更改为Canvas。

2) I have used these two lines to set the position of the rect on the Canvas. 2)我用这两行来设置矩形在画布上的位置。 You can change this to any hardcoded/programmatically calculated value. 您可以将其更改为任何硬编码/以编程方式计算的值。

Canvas.SetLeft(rect, 200);
Canvas.SetTop(rect, 20);

3) I have calculated the X and Y positions of the line here 3)我在这里计算了线的X和Y位置

line.X1 = Canvas.GetLeft(rect) + rect.Width / 2;
line.X2 = Canvas.GetLeft(rect) + rect.Width / 2;

line.Y1 = Canvas.GetTop(rect);
line.Y2 = Canvas.GetTop(rect) + rect.Height;

The X1 and X2 positions are basically the Left position of the rect plus half of the rect width X1和X2位置基本上是矩形的左侧位置加上矩形宽度的一半

The Y1 and Y2 positions are the rect top and rect top plus rect height. Y1和Y2位置是rect top和rect top加rect高度。

Add these two elements to the Canvas and it will work 将这两个元素添加到画布中,它将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM