Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个类在WPF中绘制屏幕矩形_C#_Wpf_Vb.net - Fatal编程技术网

C# 从另一个类在WPF中绘制屏幕矩形

C# 从另一个类在WPF中绘制屏幕矩形,c#,wpf,vb.net,C#,Wpf,Vb.net,所以我在主类中有一个方法,它是一个类柱,它创建两个矩形并设置它们的宽度和高度。然后我尝试使用Canvas.SetTop将它们绘制到屏幕上 这是支柱类 Class Pillar Property Right As Integer Public top, bottom As Rectangle Private gap As Integer = 60 Private _width = 100 Private gapPos As Integer Publ

所以我在主类中有一个方法,它是一个类柱,它创建两个矩形并设置它们的宽度和高度。然后我尝试使用Canvas.SetTop将它们绘制到屏幕上

这是支柱类

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)
尝试将rectange添加到画布,例如通过调用

myCanvas.Children.Add(pillar.top)

问题是屏幕上没有绘制矩形,而程序没有抛出任何错误。您应该将这些信息编辑到问题中。您是否将矩形添加到画图画布?谢谢,完全忘记添加到画布了
myCanvas.Children.Add(pillar.top)