I'm trying to create an Avalonia App which uses several UserControl Views by binding the content in the MainWindow to a property Content in the MainWindowViewModel.(I believe this is standard for MVVM).
I want to be able to handle user inputs such as tapping the screen, pressing buttons etc, while also letting those events to bubble up to the MainWindow so that all inputs from the user can be monitored. This would allow a generic handler which could be simply used update an inactivity timer.
The furthest events seems to be within the same UserControl however I would like to handle the event from the MainWindow. This would mean that everytime a new UserControl is created, the inactivity timer would still function without major changes.
Here is how I'm currently handling tap events on the UserControls:
public static event EventHandler<RoutedEventArgs> TapRegistered;
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
private void RegisterTap(object sender, RoutedEventArgs e) => IdleWindowView.TapRegistered?.Invoke(this, e);
I'm then simply picking that up on the ViewModel the UserControl.
Let me know if there's any information which could be helpful - I wasn't exactly sure what code to add.
I was able to detect taps on the MainWindow (And all views), by adding a handler in the code-behind of the MainWindow as shown below:
this.AddHandler(TappedEvent, this.RegisterTap, handledEventsToo: true);
And simply sending an event to the ViewModel to update an inactivity timer as shown below:
private void RegisterTap(object sender, RoutedEventArgs e) => MainWindow.TapRegistered?.Invoke(sender, e);