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

Art*_*ott 6 wpf xaml f# prism

WPF Visualizer Visual Tree画布

canvas.Children.Add poly |> ignore

指定的视觉是

  1. 已经是另一个视觉或
  2. CompositionTarget的根.

不要以为是1),不确定2)是什么?

使用Visual Studio 2010,F#2.0,WPF,...不是XAML

cfe*_*ern 12

在没有相关代码示例的情况下诊断问题有点困难,但问题可能是您尝试将相同的多边形添加到画布'子项两次.

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

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()
Run Code Online (Sandbox Code Playgroud)

如果我添加另一个,canvas.Children.Add poly它会与您的错误消息一起崩溃.

canvas.Children.Add poly |> ignore 
canvas.Children.Add poly |> ignore //this fails, poly already exists on the canvas
Run Code Online (Sandbox Code Playgroud)

为了解决这个错误,我首先调用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
Run Code Online (Sandbox Code Playgroud)

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