指定的Visual已经是另一个Visual的子级或CompositionTarget的根 [英] Specified Visual is already a child of another Visual or the root of a CompositionTarget

查看:499
本文介绍了指定的Visual已经是另一个Visual的子级或CompositionTarget的根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF可视化器 视觉树 画布

WPF Visualizer Visual Tree canvas

canvas.Children.Add poly |>忽略

canvas.Children.Add poly |> ignore

指定的视觉是

  1. 已经是其他视觉对象或
  2. 的孩子
  3. CompositionTarget的根.

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

Don't think it's 1), not sure what 2) is?

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

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

推荐答案

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

It's a bit hard to diagnose the problem without a relevant code sample, but maybe the problem is that you tried to add the same polygon to the canvas' children twice.

这是我为重现您的错误而加粗的代码:

This is the code I burped up 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()

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

If I add another canvas.Children.Add poly it crashes with your error message.

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

为了解决该错误,我首先打电话给canvas.Children.Remove,以删除存在的特定子项,以便将其替换为另一个子项.

In order to fix the error, I first called canvas.Children.Remove to remove the specific child that was present in order to replace it by another.

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

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

I hope this fixes your problem.

这篇关于指定的Visual已经是另一个Visual的子级或CompositionTarget的根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆