从另一个 class 在 WPF 中将矩形绘制到屏幕上
Drawing rectangles to the screen in WPF from another class
所以我在主 class 中有一个方法 class Pillar
,它创建两个矩形并设置它们的宽度和高度。然后我尝试使用 Canvas.SetTop
.
将它们绘制到屏幕上
'Main
'place rectangles
Canvas.SetTop(pillar.top, 0)
Canvas.SetBottom(pillar.bottom, 0)
Canvas.SetLeft(pillar.top, 100)
Canvas.SetLeft(pillar.bottom, 100)
这是支柱class
Class Pillar
Property Right As Integer
Public top, bottom As Rectangle
Private gap As Integer = 60
Private _width = 100
Private gapPos As Integer
Public Sub New()
top = New Rectangle
bottom = New Rectangle
top.Width = _width
bottom.Width = _width
gapPos = CInt(Math.Ceiling(Rnd() * 80)) + 470
top.Height = gapPos - (gap / 2)
bottom.Height = gapPos - (gap / 2)
top.Fill = New SolidColorBrush(Color.FromRgb(255, 255, 255))
bottom.Fill = New SolidColorBrush(Color.FromRgb(255, 255, 255))
End Sub
End Class
问题是没有在屏幕上绘制矩形,而程序没有抛出任何错误。
Canvas.SetTop/Bottom/Left/Right()
不在屏幕上绘制内容,而是定位已经显示在屏幕上的元素。所以,在调用
之前
Canvas.SetTop(pillar.top, 0)
尝试将矩形添加到 canvas,例如通过调用
myCanvas.Children.Add(pillar.top)
所以我在主 class 中有一个方法 class Pillar
,它创建两个矩形并设置它们的宽度和高度。然后我尝试使用 Canvas.SetTop
.
'Main
'place rectangles
Canvas.SetTop(pillar.top, 0)
Canvas.SetBottom(pillar.bottom, 0)
Canvas.SetLeft(pillar.top, 100)
Canvas.SetLeft(pillar.bottom, 100)
这是支柱class
Class Pillar
Property Right As Integer
Public top, bottom As Rectangle
Private gap As Integer = 60
Private _width = 100
Private gapPos As Integer
Public Sub New()
top = New Rectangle
bottom = New Rectangle
top.Width = _width
bottom.Width = _width
gapPos = CInt(Math.Ceiling(Rnd() * 80)) + 470
top.Height = gapPos - (gap / 2)
bottom.Height = gapPos - (gap / 2)
top.Fill = New SolidColorBrush(Color.FromRgb(255, 255, 255))
bottom.Fill = New SolidColorBrush(Color.FromRgb(255, 255, 255))
End Sub
End Class
问题是没有在屏幕上绘制矩形,而程序没有抛出任何错误。
Canvas.SetTop/Bottom/Left/Right()
不在屏幕上绘制内容,而是定位已经显示在屏幕上的元素。所以,在调用
Canvas.SetTop(pillar.top, 0)
尝试将矩形添加到 canvas,例如通过调用
myCanvas.Children.Add(pillar.top)