如何快速更新 WPF 画布?
在我的 wpf 应用程序中,我想更新画布上的一条线(更改起点、终点坐标)并预览更改。问题是我添加了该行,但我只能看到初始和最终状态。
有没有办法让线条/画布实时更新?我想看看这条线是如何改变长度/位置的。
例如,我收到该线路的开始/结束对列表。如果我循环列表并使用对中的值更新线的坐标,我看不到中间状态。
我尝试将线条和画布的可见性设置为可见以强制它们更新,但它不起作用。如果我只是添加新行,我看不到它们是如何添加的,只是最后一步。
在下面的代码中,每当我有新点时,都会从循环中调用方法 DrawLine。
有什么建议吗?
public void DrawLine(List<Library2d.Point> points)
{
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new System.Windows.Point(points.ElementAt(0).X, points.ElementAt(0).Y);
LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new System.Windows.Point(points.ElementAt(1).X, points.ElementAt(1).Y);
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
myPathFigure.Segments = myPathSegmentCollection;
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;
if (myPath == null)
{
myPath = new Path();
myPath.Stroke = Brushes.ForestGreen;
myPath.StrokeThickness = 1;
canvas.Children.Add(myPath);
}
myPath.Data = myPathGeometry;
myPath.Visibility = Visibility.Visible;
myPath.InvalidateMeasure();
canvas.Visibility = Visibility.Visible;
canvas.InvalidateMeasure();
}
In my wpf app i want to update a line on the canvas(change the start, end point coordinates) and preview the change. The problem is that i add the line but i can only see the initial and final state.
Is there a way to make the line/canvas update itself in real time? I want to see how the line is changing length/position.
for example i receive a list of start/end pairs for the line. if i loop the list and update the coordinates of the line with the values from the pairs i can't see the intermediary states.
i tried to set the visibility for the line and canvas to visible to force them to update but it doesn't work. If i just add new lines, i can't see how they are added, just the final step.
In the code below the method DrawLine is called from a loop every time i have new points.
any suggestions?
public void DrawLine(List<Library2d.Point> points)
{
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new System.Windows.Point(points.ElementAt(0).X, points.ElementAt(0).Y);
LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new System.Windows.Point(points.ElementAt(1).X, points.ElementAt(1).Y);
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
myPathFigure.Segments = myPathSegmentCollection;
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;
if (myPath == null)
{
myPath = new Path();
myPath.Stroke = Brushes.ForestGreen;
myPath.StrokeThickness = 1;
canvas.Children.Add(myPath);
}
myPath.Data = myPathGeometry;
myPath.Visibility = Visibility.Visible;
myPath.InvalidateMeasure();
canvas.Visibility = Visibility.Visible;
canvas.InvalidateMeasure();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
渲染线程的调度程序优先级低于 UI 线程,因此您可以在 UI 线程中运行循环,立即应用所有更改,最后渲染器会对其进行尝试。
您应该考虑将线绑定到数据点并更新它们。这是一篇关于如何实现多边形的文章:http://bea.stollnitz.com /blog/?p=35 您可以根据您的需要进行调整。
更新:链接的博客现已存档,但可以在 github 上找到: https://github .com/bstollnitz/old-wpf-blog - 多边形绑定从第 32 条开始
The rendering thread has a lower dispatcher priority than the UI thread so you run through the loop in the UI thread applying all the changes at once and then finally the renderer gets a shot at it.
You should consider binding the line to the data points and update them instead. Here is an article on how to achieve this for polygons: http://bea.stollnitz.com/blog/?p=35 which you could probably adapt for your needs.
Update: The linked blog has now been archived but can be found on github under: https://github.com/bstollnitz/old-wpf-blog - the polygon binding is starting with article 32