I am trying to convert an old WPF MvvmCross project into Avalonia UI MVVM but I don't find information about how to pass the key into the ViewModel.
In MvvmCross I had a function
private async void OnKeyboardMessage(KeyboardMessage keyMessage)
{
System.Diagnostics.Debug.WriteLine("Keyboard Message from MainWindow:" + keyMessage.Key.ToString());
if (keyMessage.Key == "F5") {
UpdateInformation();
}
}
in my ViewModel with which I could get the F5 key.
In Avalonia UI I could get the key input with:
protected override void OnKeyDown(KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.Key);
base.OnKeyDown(e);
}
In the View, but I do not understand how it can be transferred to the ViewModel.
Can someone please assist?
You would typically use a KeyBinding
with an appropriate view model command:
<Window.KeyBindings>
<KeyBinding Gesture="F5" Command="{Binding UpdateInformationCommand}"/>
</Window.KeyBindings>
Assuming you are using CommunityToolkit.Mvvm
, the command could be declared like this:
using CommunityToolkit.Mvvm.Input;
...
[RelayCommand]
private void UpdateInformation()
{
...
}