如何了解 WPF 中 Canvas.Children 中对象的类型?例如,我在画布上显示了一个椭圆形和矩形。如何获得 的类型Canvas.Children[0]?我有类似的东西,但它说“给定的表达式永远不是提供的('System.Windows.Shapes.Ellipse')类型”。我需要检查一下:
if (canvas.Children[0].GetType() is System.Windows.Shapes.Ellipse)
您不能is在这里使用,因为GetType()返回一个Type然后您需要使用typeof(MSDN):
if (canvas.Children[0].GetType() == typeof(System.Windows.Shapes.Ellipse))
Run Code Online (Sandbox Code Playgroud)
或者你可以直接is使用canvas.Children[0]
if (canvas.Children[0] is System.Windows.Shapes.Ellipse)
Run Code Online (Sandbox Code Playgroud)