异常:指定的 Visual 不是此 Visual 的祖先
以下方法在该行中抛出异常:
Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
但是如果您查看代码,vb 肯定是 surfaceWindow 的子级。那么为什么这不起作用呢?
if (!isExpanded())
{
Viewbox vb = new Viewbox();
ClassMetricView metricView = new ClassMetricView();
metricView.Width = 300;
metricView.Height = 300;
metricView.ClassName = this.name;
metricView.NumberOfMetrics = 6;
metricView.LOC = this.getLoc();
metricView.FanIn = this.getFanIn();
metricView.FanOut = this.getFanOut();
metricView.buildComponent();
vb.Child = metricView;
vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));
surfaceWindow.ClassScatter.Items.Add(vb);
this.setExpanded(true);
//Create line to connect these UI elements
Point parentPosition = surfaceWindow.RootContainer.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
Line line = new Line();
line.X1 = parentPosition.X;
line.Y1 = parentPosition.Y;
line.X2 = childPosition.X;
line.Y2 = childPosition.Y;
line.Stroke = System.Windows.Media.Brushes.Black;
line.StrokeThickness = 2;
surfaceWindow.RootGrid.Children.Add(line);
}
编辑:我找到了问题的答案:
使用 TransformToAncestor 时出错:“指定的视觉对象不是此视觉对象的祖先。”
问题是,我不明白解决方案。谁能解释一下吗?
编辑2:我尝试实现这个调度程序。但仍然抛出相同的异常。任何提示都会很棒!
public void expand(SurfaceWindow1 surfaceWindow)
{
_surfaceWindow = surfaceWindow;
Logging.Logger.getInstance().log("Expand class " + name);
if (!isExpanded())
{
Viewbox vb = new Viewbox();
ClassMetricView metricView = new ClassMetricView();
metricView.Width = 300;
metricView.Height = 300;
metricView.ClassName = this.name;
metricView.NumberOfMetrics = 5;
metricView.NumberOfRevisions = 6;
metricView.MetricsName = new string[] { "LOC", "FanIn", "FanOut", "NOM", "McCabe"};
int[,] values = { { 10, 10, 10, 10, 10}, {20, 20, 20, 20, 20}, {30, 30, 30, 30, 30}, {40, 40, 40, 40, 40}, {50, 50, 50, 50, 50}, {60, 60, 60, 60, 60} };
metricView.Metrics = values;
metricView.buildComponent();
vb.Child = metricView;
vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));
surfaceWindow.ClassScatter.Items.Add(vb);
this.setExpanded(true);
//Create line to connect these UI elements
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
vb.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
SetStatus(vb);
}
));
}
));
thread.Start();
}
}
private void SetStatus(Viewbox vb)
{
Point parentPosition = _surfaceWindow.RootContainer.TransformToAncestor(_surfaceWindow).Transform(new Point(0, 0));
Point childPosition = vb.TransformToAncestor(Window.GetWindow(vb)).Transform(new Point(0, 0));
//Point childPosition = new Point(0, 0);
Line line = new Line();
line.X1 = parentPosition.X;
line.Y1 = parentPosition.Y;
line.X2 = childPosition.X;
line.Y2 = childPosition.Y;
line.Stroke = System.Windows.Media.Brushes.Black;
line.StrokeThickness = 2;
_surfaceWindow.RootGrid.Children.Add(line);
Console.WriteLine("Draw line with position: " + line.X1 + "/" + line.Y1 + "/" + line.X2 + "/" + line.Y2);
}
The following method thrwos an exception in the line:
Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
But if you look at the code, the vb is surely a child of surfaceWindow. So why isn't this working?
if (!isExpanded())
{
Viewbox vb = new Viewbox();
ClassMetricView metricView = new ClassMetricView();
metricView.Width = 300;
metricView.Height = 300;
metricView.ClassName = this.name;
metricView.NumberOfMetrics = 6;
metricView.LOC = this.getLoc();
metricView.FanIn = this.getFanIn();
metricView.FanOut = this.getFanOut();
metricView.buildComponent();
vb.Child = metricView;
vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));
surfaceWindow.ClassScatter.Items.Add(vb);
this.setExpanded(true);
//Create line to connect these UI elements
Point parentPosition = surfaceWindow.RootContainer.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
Line line = new Line();
line.X1 = parentPosition.X;
line.Y1 = parentPosition.Y;
line.X2 = childPosition.X;
line.Y2 = childPosition.Y;
line.Stroke = System.Windows.Media.Brushes.Black;
line.StrokeThickness = 2;
surfaceWindow.RootGrid.Children.Add(line);
}
EDIT: I found maybe an answer to my problem:
Error when using TransformToAncestor: "The specified Visual is not an ancestor of this Visual."
the problem is, I don't understand the solution. Can anyone explain?
EDIT 2: I tried to implement this dispatcher. But still the same exception is thrown. Any hints would be really great!
public void expand(SurfaceWindow1 surfaceWindow)
{
_surfaceWindow = surfaceWindow;
Logging.Logger.getInstance().log("Expand class " + name);
if (!isExpanded())
{
Viewbox vb = new Viewbox();
ClassMetricView metricView = new ClassMetricView();
metricView.Width = 300;
metricView.Height = 300;
metricView.ClassName = this.name;
metricView.NumberOfMetrics = 5;
metricView.NumberOfRevisions = 6;
metricView.MetricsName = new string[] { "LOC", "FanIn", "FanOut", "NOM", "McCabe"};
int[,] values = { { 10, 10, 10, 10, 10}, {20, 20, 20, 20, 20}, {30, 30, 30, 30, 30}, {40, 40, 40, 40, 40}, {50, 50, 50, 50, 50}, {60, 60, 60, 60, 60} };
metricView.Metrics = values;
metricView.buildComponent();
vb.Child = metricView;
vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));
surfaceWindow.ClassScatter.Items.Add(vb);
this.setExpanded(true);
//Create line to connect these UI elements
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
vb.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
SetStatus(vb);
}
));
}
));
thread.Start();
}
}
private void SetStatus(Viewbox vb)
{
Point parentPosition = _surfaceWindow.RootContainer.TransformToAncestor(_surfaceWindow).Transform(new Point(0, 0));
Point childPosition = vb.TransformToAncestor(Window.GetWindow(vb)).Transform(new Point(0, 0));
//Point childPosition = new Point(0, 0);
Line line = new Line();
line.X1 = parentPosition.X;
line.Y1 = parentPosition.Y;
line.X2 = childPosition.X;
line.Y2 = childPosition.Y;
line.Stroke = System.Windows.Media.Brushes.Black;
line.StrokeThickness = 2;
_surfaceWindow.RootGrid.Children.Add(line);
Console.WriteLine("Draw line with position: " + line.X1 + "/" + line.Y1 + "/" + line.X2 + "/" + line.Y2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于能够解决这个问题了。看来 ScatterViewItem 在调用后尚未添加到 VisualTree 中,
因此我向 ScatterViewItem 添加了 SizeChangedEventHandler 和 ScatterManipulationDeltaEventHandler 并在其中添加了行。
感谢 Bart,他帮助我解决了这个
问题
I was finally able to solve the problem. It seems true that the ScatterViewItem is not yet added to the VisualTree right after the call
So I added a SizeChangedEventHandler and a ScatterManipulationDeltaEventHandler to the ScatterViewItem and add the lines there.
Thanks to Bart, who helped me with this solution
and
我在将项目列表分配给 DataGrid 时遇到了同样的错误。事实证明,如果列表为空,即使列表不为空。
要解决这个问题,添加验证就足够了:
I encountered the same error while assigning a list of items to a DataGrid. Turned out that if the list was empty, even though the list wasn't null.
To solve that, adding a validation is enough: