The specified visual is already a descendant of another Visual or CompositionTarget root

WPF renderer Visual tree canvas

canvas.Children.Add poly | > ignore

Specified visual

  • already a child of another visual or
  • CompositionTarget root.

Do not think that it is 1), do not know what 2) is?

Using Visual Studio 2010, F # 2.0, WPF, ... not XAML

+6
f # wpf xaml prism
source share
1 answer

It’s a little difficult to diagnose the problem without the appropriate code, but perhaps the problem is that you tried twice to add the same polygon to the children of the canvas.

This is the code I opened to reproduce your error:

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() 

If I add another canvas.Children.Add poly , it will fail with your error message.

 canvas.Children.Add poly |> ignore canvas.Children.Add poly |> ignore //this fails, poly already exists on the canvas 

To fix the error, I first called canvas.Children.Remove to remove the specific child that was present, to replace it with another.

 canvas.Children.Add poly |> ignore canvas.Children.Remove poly canvas.Children.Add poly |> ignore //this works, because the previous version is gone 

Hope this fixes your issue.

+12
source share

All Articles