更改线路位置悬停在路径上时CPU使用率增加。 [英] CPU usage increased when change Line position hover over the Path.

查看:88
本文介绍了更改线路位置悬停在路径上时CPU使用率增加。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了路径Path并在路径形状上显示了Line。此外,将线位置悬停在面板上。在这种情况下,Line没有顺利移动,CPU性能提高到50%。 

I have created the range Path and displayed the Line over the path shapes. Also, changed the Line position hover over the panel. In this scenario, Line was not moving smoothly and CPU performance increased up to 50%. 

<Grid>
        <Canvas x:Name="Canvas" MouseMove="UIElement_OnMouseMove" > 
        </Canvas>
</Grid>


  public partial class MainWindow : Window
    {
        private Line line;
        private List<Point> areaPoints;
        public MainWindow()
        {
            InitializeComponent();

            //Create Line
            line = new Line() { Stroke = Brushes.Red, StrokeThickness = 1, Y1 = 0, Y2 = 400 };

            areaPoints = new List<Point>();
            var random = new Random();

            for (int i = 0; i < 2000; i++)
            {
                areaPoints.Add(new Point(i*0.5, random.Next(70, 150)));
                areaPoints.Add(new Point(i*0.5, random.Next(320, 400)));
            }
             //Create path and add in Canvas
            Canvas.Children.Add(CreatePath());
        }

        //Create Range path 
        private Path CreatePath()
        {
            Path segPath = new Path() { Stroke = Brushes.LightSkyBlue, StrokeThickness = 1, Fill = Brushes.LightSkyBlue };
            PathFigure figure = new PathFigure {StartPoint = areaPoints[0]};

            for (int i = 0; i < areaPoints.Count; i += 2)
            {
                LineSegment lineSeg = new LineSegment();
                lineSeg.Point = areaPoints[i];
                figure.Segments.Add(lineSeg);
            }

            for (int i = areaPoints.Count - 1; i >= 1; i -= 2)
            {
                LineSegment lineSeg = new LineSegment();
                lineSeg.Point = areaPoints[i];
                figure.Segments.Add(lineSeg);
            }

            figure.IsClosed = true;

            PathGeometry segmentGeometry = new PathGeometry();
            segmentGeometry.Figures.Add(figure);
            segPath.Data = segmentGeometry;
            return segPath;
        }

        //Change Line position based on mouse pos, but CPU usage increased upto 50 % and mouse not move soomthly. 
        private void UIElement_OnMouseMove(object sender, MouseEventArgs e)
        {
            var pos = e.GetPosition(Canvas);
            if (!Canvas.Children.Contains(line))
                Canvas.Children.Add(line);
            line.X1 = line.X2 = pos.X;

        }
    }








甚至将Path元素的IsHitTestVisible设置为false以避免与鼠标相关的操作或任何其他UI逻辑,但CPU使用率持续增加。仅当使用具有更多点的路径形状(更多线段)时才会出现此问题。但是同样的情况在UWP平台中完美地运行
。似乎与WPF相关的框架问题。 

Even setting Path element's IsHitTestVisible as false to avoid mouse related actions or any other UI logic, but CPU usage increased continuesly. This issue occurred only when use path shapes with more points(more line segment). But this same scenario working perfectly in UWP platform. It seems to WPF related framework issue. 

如何在悬停窗口时平滑地减少CPU使用率并使线路过渡?

How to reduce CPU usage and make line transition smoothly while hovering window ?

Sample_Demo

Sample_Demo

推荐答案

您好,

查看您的代码后,我可以给您两个建议。

After looking at your code, two suggestions I can give you.

1。试着 使用
StreamGeometry
来代替
PathGeometry,
StreamGeometry 是PathGeometry的轻量级替代品, ,

1. Try to  use StreamGeometry to instead PathGeometry, StreamGeometry is light-weight alternative to PathGeometry ,

https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-create-a-shape-using-a-streamgeometry

有关优化WPF应用程序性能的更多信息,您可以看到:

More about optimizing WPF Application Performance, you can see:

https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/optimizing-wpf-application绩效

2。 尝试减少代码执行的频率,以避免不必要的CPU开销。

2. Try to reduce the frequency of code execution to avoid unnecessary CPU overhead.

 private void UIElement_OnMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                var pos = e.GetPosition(Canvas);
                if (!Canvas.Children.Contains(line))
                    Canvas.Children.Add(line);
                line.X1 = line.X2 = pos.X;
            }
        }

最好的问候,

Bob


这篇关于更改线路位置悬停在路径上时CPU使用率增加。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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