C#WPF错误指定的Visual已经是另一个Visual的子项或CompositionTarget的根 [英] C# WPF error Specified Visual is already a child of another Visual or the root of a CompositionTarget

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

问题描述

好的,这是一个相当常见的错误,我知道基本上我在画布上添加了两次相同的路径。



问题是我有一个虚拟路径(让我们称之为pathDummy),它会在任何时候改变它的几何形状(即它的一组点)时多次添加到画布。这就是我必须处理的方法,并且不能一直删除它。



所以我尝试通过执行以下操作从pathDummy创建一条新路径:



System.Windows.Shapes.Path newPath = SelectedPath;

paths.Add(newPath);

plotCanvas。 Children.Add(路径[paths.Count - 1]);



但这并没有解决问题。那么什么是在newPath中保持不变并生成错误? thanx任何帮助

解决方案

您还没有从所选路径创建新路径;您刚刚将所选路径存储在一个新变量中。这给了你两个变量来查看同一个类的实例,而不是两个类的实例。



创建的新实例路径类,您需要使用 new 关键字来创建新实例,并复制相关属性:

  var  newPath =  new  System.Windows.Shapes.Path 
{
Data = SelectedPath.Data,
Fill = SelectedPath.Fill,
Stroke = SelectedPath.Stroke,
...
};


Ok this is a fairly common error and I'm aware that basically I'm adding twice the same path to the canvas.

The problem is that I have a "dummy path" (let's call it pathDummy) which is added several times to the canvas any time changing it's geometry (that is its set of points). This is how I have to deal with it and can't remove it all the times.

So I tried to make a new path from pathDummy by doing:

System.Windows.Shapes.Path newPath = SelectedPath;
paths.Add(newPath);
plotCanvas.Children.Add(paths[paths.Count - 1]);

but that didn't solve the problem. So what is that stays the same in newPath and generates the error? thanx for any help

解决方案

You haven't created a new path from the selected path; you've just stored the selected path in a new variable. That gives you two variables looking at the same instance of the class, not two instances of the class.

To create a new instance of the Path class, you need to use the new keyword to create a new instance, and copy over the relevant properties:

var newPath = new System.Windows.Shapes.Path
{
    Data = SelectedPath.Data,
    Fill = SelectedPath.Fill,
    Stroke = SelectedPath.Stroke,
    ...
};


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

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