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

发布于 2024-09-04 00:00:28 字数 235 浏览 4 评论 0原文

WPF 可视化工具 视觉树 画布

canvas.Children.Add Poly |>忽略

指定的 Visual

  1. 已经是另一个 Visual 的子级或
  2. CompositionTarget 的根。

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

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

WPF Visualizer
Visual Tree
canvas

canvas.Children.Add poly |> ignore

Specified Visual is

  1. already a child of another Visual or
  2. the root of a CompositionTarget.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撞了怀 2024-09-11 00:00:28

如果没有相关的代码示例,诊断问题有点困难,但问题可能是您尝试将相同的多边形添加到画布的子级两次。

这是我为了重现您的错误而打嗝的代码:

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

我希望这能解决您的问题。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文