gpt4 book ai didi

c# - 异常:指定的 Visual 不是此 Visual 的祖先

转载 作者:太空宇宙 更新时间:2023-11-03 22:18:36 27 4
gpt4 key购买 nike

下面的方法在行中抛出一个异常:

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);
}

编辑:我找到了我的问题的答案:

Error when using TransformToAncestor: "The specified Visual is not an ancestor of this Visual."

问题是,我不明白解决方案。谁能解释一下?

编辑 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);
}

最佳答案

我终于能够解决问题了。在调用之后 ScatterViewItem 似乎还没有立即添加到 VisualTree 中似乎是真的

surfaceWindow.ClassScatter.Item.Add(vb);

所以我向 ScatterViewItem 添加了一个 SizeChangedEventHandler 和一个 ScatterManipulationDeltaEventHandler 并在其中添加了行。

感谢Bart , 谁帮我解决了这个问题

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

ScatterViewItem svItem = _surfaceWindow.ClassScatter.ItemContainerGenerator.ContainerFromItem(vb) as ScatterViewItem;
svItem.Tag = this.name;
svItem.AddHandler(ScatterViewItem.ScatterManipulationDeltaEvent, new ScatterManipulationDeltaEventHandler(MovementHandler));
svItem.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(ScatterSizeChanged));

this.setExpanded(true);
}

public void MovementHandler(object sender, ScatterManipulationDeltaEventArgs e)
{
updateConnectingLines(sender);
}

public void ScatterSizeChanged(object sender, SizeChangedEventArgs e)
{
updateConnectingLines(sender);
}

private void updateConnectingLines(object sender)
{
removeOldLines((sender as ScatterViewItem).Tag as String);

Point childPosition = (sender as ScatterViewItem).TransformToAncestor(_surfaceWindow.ClassScatter).Transform(new Point(0, 0));
Point parentPosition = _surfaceWindow.RootContainer.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;
line.Tag = this.name;
lines.Add(line);

_surfaceWindow.RootGrid.Children.Add(line);
}

关于c# - 异常:指定的 Visual 不是此 Visual 的祖先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4041714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com