Primary WinForms APIs:
Click, TextChanged, KeyDown)KeyPreview, ProcessCmdKey)Primary Avalonia APIs:
ICommand on command sources (Button, MenuItem)KeyBinding, KeyGesture, HotKeyPointerPressed, KeyDown)| WinForms | Avalonia |
|---|---|
button.Click += ... |
Button Command="{CompiledBinding ...}" |
TextBox.TextChanged handler |
bind Text to view-model property and react in model layer |
imperative Enabled toggling |
bind IsEnabled to state or command CanExecute |
| WinForms | Avalonia |
|---|---|
KeyPreview = true + KeyDown |
root-level KeyBindings |
ProcessCmdKey |
KeyBinding with Gesture + command |
| menu shortcut text | InputGesture on MenuItem |
| shortcut execution | HotKey or KeyBinding |
WinForms C#:
public MainForm()
{
InitializeComponent();
KeyPreview = true;
saveButton.Click += (_, _) => Save();
KeyDown += (_, e) =>
{
if (e.Control && e.KeyCode == Keys.S)
{
Save();
e.Handled = true;
}
};
}
Avalonia XAML:
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:MyApp.ViewModels"
x:DataType="vm:EditorViewModel">
<UserControl.KeyBindings>
<KeyBinding Gesture="Ctrl+S" Command="{CompiledBinding SaveCommand}" />
</UserControl.KeyBindings>
<StackPanel Spacing="8">
<Button Content="Save"
Command="{CompiledBinding SaveCommand}"
HotKey="Ctrl+S" />
</StackPanel>
</UserControl>
using Avalonia.Controls;
using Avalonia.Input;
var view = new UserControl();
view.KeyBindings.Add(new KeyBinding
{
Gesture = KeyGesture.Parse("Ctrl+S"),
Command = viewModel.SaveCommand
});
var saveButton = new Button
{
Content = "Save",
Command = viewModel.SaveCommand,
HotKey = KeyGesture.Parse("Ctrl+S")
};
InputGesture is display-only; wire HotKey or KeyBinding for execution.