模型失效时未捕获 Oxyplot 的鼠标事件
Oxyplot's mouse events are not caught while model invalidated
短:OxyPlot鼠标事件处理程序(例如mySeries.MouseDown)在我调用myModel.InvalidatePlot(true/false) 来更新我的模型。
详细:我正在以一种非常规的方式使用 OxyPlot 库。我正在通过快速更新占据整个图表的图像注释来模仿在后台播放视频。这允许我使用鼠标事件在图像上绘图。我的问题是 video/image 注释播放和鼠标事件是分开工作的,但是当鼠标事件在 'frame updates' 期间发生时,其中一些会被遗漏。我的信念是,当绘图无效时 myModel.InvalidatePlot(true/false),在模型更新之前不会拾取鼠标事件。
我发现 InvalidatePlot 导致了鼠标按钮事件(例如 OnMouseDown(MouseButtonEventArgs e))从未被 PlotView 捕获的问题。为了解决这个问题,我使用了另一个 WPF 对象来捕获鼠标事件并强制调用我的 PlotView 的鼠标处理方法:
1) 我在另一个 ZIndex 级别上添加了一个 WPF 网格。并将函数附加到鼠标事件(MouseDown、MouseWheel 等):
<Grid x:Name="Grid_MouseCatcher"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Panel.ZIndex="2"
MouseDown="Grid_MouseCatcher_MouseDown"
MouseEnter="Grid_MouseCatcher_MouseEnter"
MouseLeave="Grid_MouseCatcher_MouseLeave"
MouseMove="Grid_MouseCatcher_MouseMove"
MouseUp="Grid_MouseCatcher_MouseUp"
MouseWheel="Grid_MouseCatcher_MouseWheel"
Opacity="0.0"
PreviewMouseMove="SetFocusToOxyPlot" />
<oxy:PlotView x:Name="PlotView"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Panel.ZIndex="1"
Model="{Binding PlotModel}" />
2) 网格处理的每个事件都会调用 public 我在 OxyPlot 源代码中创建的方法:
/// <summary>
/// Sends mouse events from the overlaying grid to the PlotView. Forcing an Action.
/// This was included due to an OxyPlot bug where the OnMouse... methods were not being called
/// when the plot was invalidated via InvalidatePlot(...)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Grid_MouseCatcher_MouseDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("MouseCatcher - Down: " + e.GetPosition(Grid_MouseCatcher).X + " " + e.GetPosition(Grid_MouseCatcher).Y);
PlotView.ForceMouseDown(e);
}
3) 在源代码文件 OxyPlot.Wpf\PlotBase.Events.cs 中,我创建了明确调用的函数(例如 ForceMouseDown(MouseButtonEventArgs e))受保护的鼠标事件 (OnMouseDown(MouseButtonEventArgs e))
/// <summary>
/// Forces a call to Mouse handler.
/// This was needed because the events were getting lost when the model was updating
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public void ForceMouseDown(MouseButtonEventArgs e){
this.OnMouseDown(e);
}
/// <summary>
/// Invoked when an unhandled MouseDown attached event reaches an element in its route that is derived from this class.
/// Implement this method to add class handling for this event.
/// </summary>
/// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs" /> that contains the event data.
/// This event data reports details about the mouse button that was pressed and the handled state.</param>
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (e.Handled)
{
return;
}
this.Focus();
this.CaptureMouse();
// store the mouse down point, check it when mouse button is released to determine if the context menu should be shown
this.mouseDownPoint = e.GetPosition(this).ToScreenPoint();
e.Handled = this.ActualController.HandleMouseDown(this, e.ToMouseDownEventArgs(this));
}
通过这种方式解决问题,我仍然可以按照 OxyPlot 推荐的方式将鼠标事件处理程序添加到我的 Plotview,因为它们的 OnMouse#### 事件仍在被调用,它们只是被覆盖网格显式调用.
基于 CodyF 的修复,我找到了一个更简单的修复。在 OxyPlot.Wpf.PlotBase 中的 OnApplyTemplate() 末尾添加以下行:
var mouseGrid = new Grid();
mouseGrid.Background = Brushes.Transparent; // background must be set for hit test to work
this.grid.Children.Add(mouseGrid);
但是,此更改目前会阻止工具提示。另请参阅 OxyPlot github 页面上的 pull request。
短:OxyPlot鼠标事件处理程序(例如mySeries.MouseDown)在我调用myModel.InvalidatePlot(true/false) 来更新我的模型。
详细:我正在以一种非常规的方式使用 OxyPlot 库。我正在通过快速更新占据整个图表的图像注释来模仿在后台播放视频。这允许我使用鼠标事件在图像上绘图。我的问题是 video/image 注释播放和鼠标事件是分开工作的,但是当鼠标事件在 'frame updates' 期间发生时,其中一些会被遗漏。我的信念是,当绘图无效时 myModel.InvalidatePlot(true/false),在模型更新之前不会拾取鼠标事件。
我发现 InvalidatePlot 导致了鼠标按钮事件(例如 OnMouseDown(MouseButtonEventArgs e))从未被 PlotView 捕获的问题。为了解决这个问题,我使用了另一个 WPF 对象来捕获鼠标事件并强制调用我的 PlotView 的鼠标处理方法:
1) 我在另一个 ZIndex 级别上添加了一个 WPF 网格。并将函数附加到鼠标事件(MouseDown、MouseWheel 等):
<Grid x:Name="Grid_MouseCatcher"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Panel.ZIndex="2"
MouseDown="Grid_MouseCatcher_MouseDown"
MouseEnter="Grid_MouseCatcher_MouseEnter"
MouseLeave="Grid_MouseCatcher_MouseLeave"
MouseMove="Grid_MouseCatcher_MouseMove"
MouseUp="Grid_MouseCatcher_MouseUp"
MouseWheel="Grid_MouseCatcher_MouseWheel"
Opacity="0.0"
PreviewMouseMove="SetFocusToOxyPlot" />
<oxy:PlotView x:Name="PlotView"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Panel.ZIndex="1"
Model="{Binding PlotModel}" />
2) 网格处理的每个事件都会调用 public 我在 OxyPlot 源代码中创建的方法:
/// <summary>
/// Sends mouse events from the overlaying grid to the PlotView. Forcing an Action.
/// This was included due to an OxyPlot bug where the OnMouse... methods were not being called
/// when the plot was invalidated via InvalidatePlot(...)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Grid_MouseCatcher_MouseDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("MouseCatcher - Down: " + e.GetPosition(Grid_MouseCatcher).X + " " + e.GetPosition(Grid_MouseCatcher).Y);
PlotView.ForceMouseDown(e);
}
3) 在源代码文件 OxyPlot.Wpf\PlotBase.Events.cs 中,我创建了明确调用的函数(例如 ForceMouseDown(MouseButtonEventArgs e))受保护的鼠标事件 (OnMouseDown(MouseButtonEventArgs e))
/// <summary>
/// Forces a call to Mouse handler.
/// This was needed because the events were getting lost when the model was updating
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public void ForceMouseDown(MouseButtonEventArgs e){
this.OnMouseDown(e);
}
/// <summary>
/// Invoked when an unhandled MouseDown attached event reaches an element in its route that is derived from this class.
/// Implement this method to add class handling for this event.
/// </summary>
/// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs" /> that contains the event data.
/// This event data reports details about the mouse button that was pressed and the handled state.</param>
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (e.Handled)
{
return;
}
this.Focus();
this.CaptureMouse();
// store the mouse down point, check it when mouse button is released to determine if the context menu should be shown
this.mouseDownPoint = e.GetPosition(this).ToScreenPoint();
e.Handled = this.ActualController.HandleMouseDown(this, e.ToMouseDownEventArgs(this));
}
通过这种方式解决问题,我仍然可以按照 OxyPlot 推荐的方式将鼠标事件处理程序添加到我的 Plotview,因为它们的 OnMouse#### 事件仍在被调用,它们只是被覆盖网格显式调用.
基于 CodyF 的修复,我找到了一个更简单的修复。在 OxyPlot.Wpf.PlotBase 中的 OnApplyTemplate() 末尾添加以下行:
var mouseGrid = new Grid();
mouseGrid.Background = Brushes.Transparent; // background must be set for hit test to work
this.grid.Children.Add(mouseGrid);
但是,此更改目前会阻止工具提示。另请参阅 OxyPlot github 页面上的 pull request。