有没有可能在AvaloniaUI中全局捕获鼠标按下按钮事件(包括上下)?以便在任何控件之外(或者可能在任何特定视图模型之外)被通知这些事件?
您可以在MainWindow
的代码后台中监听这些事件,并处理已由其他控件处理过的事件,使用 handledEventsToo: true
:
public class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
this.AddHandler(PointerPressedEvent, MouseDownHandler, handledEventsToo: true);
this.AddHandler(PointerReleasedEvent, MouseUpHandler, handledEventsToo: true);
#if DEBUG
this.AttachDevTools();
#endif
}
private void MouseUpHandler(object sender, PointerReleasedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Mouse released.");
}
private void MouseDownHandler(object sender, PointerPressedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Mouse pressed.");
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}