github编辑

快捷键

Various Controls that implement ICommandSource have a HotKey property that you can set or bind to. Pressing the hotkey will execute the command boundarrow-up-right to the Control.

<Menu>
    <MenuItem Header="_File">
        <MenuItem x:Name="SaveMenuItem" Header="_Save" Command="{Binding SaveCommand}" HotKey="Ctrl+S"/>
    </MenuItem>
</Menu>

You can also use the static methods of the HotKeyManager class to set and get hotkeys from code:

InitializeComponent();

var saveMenuItem = this.FindControl<Avalonia.Controls.MenuItem>("SaveMenuItem");

HotKeyManager.SetHotKey(saveMenuItem, new KeyGesture(Key.S, KeyModifiers.Control));

See the Locating Controlsarrow-up-right section for more information on finding named Controls with the FindControl method.

Keys and Modifiers

A Hotkey must have one Keyarrow-up-right and zero or more KeyModifiersarrow-up-right. When setting a Hotkey in XAML using the HotKey property, the string will be parsed as a KeyGesturearrow-up-right. Enum.Parsearrow-up-right is used to parse the key and modifiers but synonyms like Ctrl instead of Control or Win instead of Meta can be used.

Reference

Source code

最后更新于