简体   繁体   English

如何在 Avalonia MVVM 应用程序中使用 InputGesture 实现 MenuItem?

[英]How to implement MenuItem with InputGesture in an Avalonia MVVM Application?

I need a Menu with InputGesture like in the picture here: https://github.com/AvaloniaUI/Avalonia/pull/3602我需要一个带有 InputGesture 的菜单,如下图所示: https://github.com/AvaloniaUI/Avalonia/pull/3602

Starting in Microsoft Visual Studio Community 2019 with a new Avalonia MVVM Application, I try to set up the XAML in Visual Studio to achieve this:从 Microsoft Visual Studio Community 2019 开始,使用新的 Avalonia MVVM 应用程序,我尝试在 Visual Studio 中设置 XAML 以实现此目的:

<MenuItem Header="New" InputGesture="CTRL + N" />

This line then results in the error message:然后,此行会导致错误消息:

Unable to resolve suitable regular or attached property InputGesture on type Avalonia.Controls:Avalonia.Controls.MenuItem

I found a working sample here: https://github.com/AvaloniaUI/XamlControlsGallery我在这里找到了一个工作示例: https://github.com/AvaloniaUI/XamlControlsGallery

I'd try to build my Solution and Project based on that example to get it working.我会尝试根据该示例构建我的解决方案和项目以使其正常工作。 But I would prefer being able to implement it on a new Avalonia MVVM Application build from scratch with Visual Studio.但我更希望能够在使用 Visual Studio 从头开始构建的新 Avalonia MVVM 应用程序上实现它。 Hints are very appreciated.非常感谢提示。

InputGesture is available starting with 0.10.0-preview1 , it's not available in the stable 0.9.x branch. InputGesture0.10.0-preview1开始可用,但在稳定的0.9.x分支中不可用。 XamlControlsGallery is using the preview version. XamlControlsGallery 正在使用预览版。

For the time being the new features are not documented in Avalonia Docs , it may be useful for others to see my findings.目前Avalonia Docs中没有记录新功能,其他人看到我的发现可能会很有用。
My goal was to create menus with shortcuts containing numbers (eg Alt+0 ).我的目标是创建带有包含数字的快捷方式的菜单(例如Alt+0 )。
In pure WPF (without Avalonia) the value "Alt+0" can be assigned to the tags representing a menu with shortcut.在纯 WPF(没有 Avalonia)中,可以将值"Alt+0"分配给代表具有快捷方式的菜单的标签。
In Avalonia however the values must correspond to the enum Key in Key.cs .然而,在 Avalonia 中,这些值必须对应于Key.cs中的enum Key
Otherwise the mentioned example menu would display Alt+None instead of Alt+0 , and command binding wouldn't work as expected.否则,上述示例菜单将显示Alt+None而不是Alt+0 ,并且命令绑定将无法按预期工作。
This is an example XAML line to display the menu and bind the command as desired:这是一个示例 XAML 行,用于显示菜单并根据需要绑定命令:

<MenuItem Header="Menu 0" InputGesture="Alt+D0" HotKey="Alt+D0" Command="{Binding Alt0Command}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM