指定的 Visual 已经是另一个 Visual 的子级或 CompositionTarget 的根。

7

WPF可视化器

可视树

画布

canvas.Children.Add poly |> ignore

指定的可视对象是:

  1. 已经是另一个可视对象的子元素,或者
  2. 是CompositionTarget的根元素。

看起来不像是1),不确定2)是什么?

使用Visual Studio 2010、F# 2.0、WPF...而非XAML

1个回答

14

没有相关的代码示例,所以诊断问题有些困难,但也许问题在于您试图将相同的多边形添加到画布的子项中两次。

这是我为了重现您的错误而编写的代码:

type SimpleWindow() as this =
    inherit Window()

    do
        let makepoly size corners =
            let size = 192.0
            let angle = 2.0 * Math.PI / float corners
            let getcoords size angle = new Point(size * cos angle, size * sin angle)

            let poly = new Polygon(Fill = Brushes.Red)
            poly.Points <- new PointCollection([for i in 0..corners-1 -> getcoords size (float i * angle)])
            poly

        let canvas = new Canvas(HorizontalAlignment = HorizontalAlignment.Center,
                                VerticalAlignment = VerticalAlignment.Center)

        let poly = makepoly 192.0 5
        Canvas.SetLeft(poly, canvas.Width / 2.0)
        Canvas.SetTop(poly, canvas.Width / 2.0)

        canvas.Children.Add poly |> ignore //this works
        this.AddChild canvas |> ignore

SimpleWindow().Show()
如果我添加另一个canvas.Children.Add poly,它会崩溃并显示您的错误信息。
canvas.Children.Add poly |> ignore 
canvas.Children.Add poly |> ignore //this fails, poly already exists on the canvas
为了修复这个错误,我首先调用了canvas.Children.Remove来删除特定的子元素,以便将其替换为另一个。
canvas.Children.Add poly |> ignore 
canvas.Children.Remove poly
canvas.Children.Add poly |> ignore //this works, because the previous version is gone

我希望这可以解决你的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接