Ign*_*cia 13 c# wpf performance events
我已经完成了自定义图表控件,我想在光标后面绘制一个简单的交叉.该图表在Canvas上实现为PolyLine,我在Canvas的OnMouseMove事件中绘制两条线来改变它们的坐标.
The surprise was to found that the event get called only each 10 seconds or so event when the MainGUI thread is idle (the UI is completely responsive and if I pause the application the main thread is at the App mainForm.ShowDialog()).
Any idea on how to find why is this happening? I get the same performance using the OnMouseMove or the PreviewOnMouseMove.
EDIT: This is how I paint the cross (anyway if I put a breakpoint at the OnMouseMove it only stops from time to time).
XAML:
<Border BorderThickness="1" BorderBrush="White" Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2" >
<Canvas x:Name="DrawArea" PreviewMouseMove="DrawArea_PreviewMouseMove" />
</Border>
Run Code Online (Sandbox Code Playgroud)
CS:
public Chart()
{
_line = new Polyline();
_line.Stroke = Brushes.Orange;
_crossX = new Line();
_crossY = new Line();
_crossX.Stroke = Brushes.Orange;
_crossY.Stroke = Brushes.Orange;
_crossX.StrokeThickness = 1;
_crossY.StrokeThickness = 1;
InitializeComponent();
this.DrawArea.Children.Add(_line);
this.DrawArea.Children.Add(_crossX);
this.DrawArea.Children.Add(_crossY);
}
private void DrawArea_MouseMove(object sender, MouseEventArgs e)
{
Point mousePosition = System.Windows.Input.Mouse.GetPosition(this.DrawArea);
_crossX.X1 = 0;
_crossX.X2 = this.DrawArea.ActualWidth;
_crossX.Y1 = _crossX.Y2 = mousePosition.Y;
_crossY.Y1 = 0;
_crossY.Y2 = this.DrawArea.ActualHeight;
_crossY.X1 = _crossY.X2 = mousePosition.X;
}
Run Code Online (Sandbox Code Playgroud)
WPF*_*-it 28
这很奇怪,我不知道为什么......
FrameworkElement.MouseMove 仅当区域有一些明确的背景画笔\填充集时才有效.
在您的情况下设置Canvas.Background="Transparent",它应该工作.
还有另一个解决方法...... WPF在CaptureMouse()之后不发送MouseMove事件;
这可能是因为它HitTest取决于彩色像素及其反馈.
无论是什么,它都没有在MSDN上记录下来,并且让许多UI设计者感到困惑.