gpt4 book ai didi

wpf - 如何显示菜单项的工作键盘快捷键?

转载 作者:行者123 更新时间:2023-12-03 12:51:28 24 4
gpt4 key购买 nike

我正在尝试使用具有键盘快捷键的菜单项来创建可本地化的WPF菜单栏-而不是加速键/助记符(通常显示为带下划线的字符,可以在菜单已打开时按下以直接选择菜单项),但是键盘快捷键(通常是Ctrl +另一个键的组合),显示在菜单项标题旁边右对齐。

我正在为我的应用程序使用MVVM模式,这意味着我避免在可能的情况下将任何代码放在代码的后面,并让我的 View 模型(我分配给 DataContext properties)提供 ICommand interface的实现,这些实现由 View 中的控件使用。

作为重现此问题的基础,这里是一些如上所述的应用程序最小源代码:

Window1.xaml

<Window x:Class="MenuShortcutTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MenuShortcutTest" Height="300" Width="300">
<Menu>
<MenuItem Header="{Binding MenuHeader}">
<MenuItem Header="{Binding DoSomethingHeader}" Command="{Binding DoSomething}"/>
</MenuItem>
</Menu>
</Window>

Window1.xaml.cs
using System;
using System.Windows;

namespace MenuShortcutTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

this.DataContext = new MainViewModel();
}
}
}

MainViewModel.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace MenuShortcutTest
{
public class MainViewModel
{
public string MenuHeader {
get {
// in real code: load this string from localization
return "Menu";
}
}

public string DoSomethingHeader {
get {
// in real code: load this string from localization
return "Do Something";
}
}

private class DoSomethingCommand : ICommand
{
public DoSomethingCommand(MainViewModel owner)
{
if (owner == null) {
throw new ArgumentNullException("owner");
}

this.owner = owner;
}

private readonly MainViewModel owner;

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)
{
// in real code: do something meaningful with the view-model
MessageBox.Show(owner.GetType().FullName);
}

public bool CanExecute(object parameter)
{
return true;
}
}

private ICommand doSomething;

public ICommand DoSomething {
get {
if (doSomething == null) {
doSomething = new DoSomethingCommand(this);
}

return doSomething;
}
}
}
}

WPF MenuItem class具有 InputGestureText property,但是如SO问题(如 thisthisthisthis)中所述,它纯粹是修饰性的,对应用程序实际处理的快捷方式没有任何影响。

诸如 thisthis之类的问题指出,该命令应与窗口的 KeyBinding 列表中的 InputBindings 链接。在启用该功能的同时,它不会自动显示带有菜单项的快捷方式。 Window1.xaml 更改如下:
<Window x:Class="MenuShortcutTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MenuShortcutTest" Height="300" Width="300">
<Window.InputBindings>
<KeyBinding Key="D" Modifiers="Control" Command="{Binding DoSomething}"/>
</Window.InputBindings>
<Menu>
<MenuItem Header="{Binding MenuHeader}">
<MenuItem Header="{Binding DoSomethingHeader}" Command="{Binding DoSomething}"/>
</MenuItem>
</Menu>
</Window>

我还尝试过手动设置 InputGestureText属性,使 Window1.xaml 看起来像这样:
<Window x:Class="MenuShortcutTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MenuShortcutTest" Height="300" Width="300">
<Window.InputBindings>
<KeyBinding Key="D" Modifiers="Control" Command="{Binding DoSomething}"/>
</Window.InputBindings>
<Menu>
<MenuItem Header="{Binding MenuHeader}">
<MenuItem Header="{Binding DoSomethingHeader}" Command="{Binding DoSomething}" InputGestureText="Ctrl+D"/>
</MenuItem>
</Menu>
</Window>

这确实显示了快捷方式,但是由于显而易见的原因,它不是可行的解决方案:
  • 当实际的快捷方式绑定(bind)更改时,它不会更新,因此即使用户无法配置快捷方式,此解决方案也是维护的噩梦。
  • 文本需要本地化(例如Ctrl键在某些语言中具有不同的名称),因此,如果更改了任何快捷方式,则所有翻译都需要单独更新。

  • 我已经研究过创建一个 IValueConverter 来将 InputGestureText属性绑定(bind)到窗口的 InputBindings 列表(在 KeyBinding列表中可能有多个 InputBindings,或者根本没有,所以我没有可以绑定(bind)的特定 KeyBinding实例到(如果 KeyBinding甚至使其成为绑定(bind)目标))。在我看来,这是最理想的解决方案,因为它非常灵活并且同时非常干净(它不需要在各个地方进行过多的声明),但是一方面, InputBindingCollection 并未实现 INotifyCollectionChanged ,因此当替换快捷方式时,绑定(bind)将不会更新,另一方面,我没有以整洁的方式为转换器提供对我的 View 模型的引用(它需要访问本地化数据)。而且, InputBindings 不是依赖项属性,因此我也无法将其绑定(bind)到 ItemGestureText属性也可以绑定(bind)到的公共(public)源(例如位于 View 模型中的输入绑定(bind)列表)。

    现在,许多资源( this questionthat questionthis threadthat questionthat thread都指出 RoutedCommand RoutedUICommand 包含内置的 InputGestures property,这意味着来自该属性的键绑定(bind)会自动显示在菜单项中。

    但是,使用这两种 ICommand实现似乎打开了一个新的蠕虫 jar ,因为它们的 Execute CanExecute 方法不是虚拟的,因此不能在子类中覆盖以填充所需的功能。提供此功能的唯一方法似乎是在XAML中声明一个 CommandBinding (例如 herehere所示),该命令将命令与事件处理程序连接-但是,该事件处理程序将位于后面的代码中,从而违反了MVVM体系结构如上所述。

    尽管如此,这意味着将上述大多数结构从内到外翻转(这也意味着我需要下定决心,如何在当前的,相对早期的开发阶段最终解决问题):

    Window1.xaml
    <Window x:Class="MenuShortcutTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MenuShortcutTest"
    Title="MenuShortcutTest" Height="300" Width="300">
    <Window.CommandBindings>
    <CommandBinding Command="{x:Static local:DoSomethingCommand.Instance}" Executed="CommandBinding_Executed"/>
    </Window.CommandBindings>
    <Menu>
    <MenuItem Header="{Binding MenuHeader}">
    <MenuItem Header="{Binding DoSomethingHeader}" Command="{x:Static local:DoSomethingCommand.Instance}"/>
    </MenuItem>
    </Menu>
    </Window>

    Window1.xaml.cs
    using System;
    using System.Windows;

    namespace MenuShortcutTest
    {
    public partial class Window1 : Window
    {
    public Window1()
    {
    InitializeComponent();

    this.DataContext = new MainViewModel();
    }

    void CommandBinding_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
    {
    ((MainViewModel)DataContext).DoSomething();
    }
    }
    }

    MainViewModel.cs
    using System;
    using System.Windows;
    using System.Windows.Input;

    namespace MenuShortcutTest
    {
    public class MainViewModel
    {
    public string MenuHeader {
    get {
    // in real code: load this string from localization
    return "Menu";
    }
    }

    public string DoSomethingHeader {
    get {
    // in real code: load this string from localization
    return "Do Something";
    }
    }

    public void DoSomething()
    {
    // in real code: do something meaningful with the view-model
    MessageBox.Show(this.GetType().FullName);
    }
    }
    }

    DoSomethingCommand.cs
    using System;
    using System.Windows.Input;

    namespace MenuShortcutTest
    {
    public class DoSomethingCommand : RoutedCommand
    {
    public DoSomethingCommand()
    {
    this.InputGestures.Add(new KeyGesture(Key.D, ModifierKeys.Control));
    }

    private static Lazy<DoSomethingCommand> instance = new Lazy<DoSomethingCommand>();

    public static DoSomethingCommand Instance {
    get {
    return instance.Value;
    }
    }
    }
    }

    出于相同的原因( RoutedCommand.Execute等是非虚拟的),我不知道如何像创建基于 RoutedCommandin an answer to this question一样使用 RelayCommand来对 RoutedCommand进行子类化,因此我不必绕过 InputBindings窗口的外观-在 ICommand子类中显式重新实现 RoutedCommand的方法时,感觉像是在破坏某些东西。

    而且,虽然快捷方式是使用 RoutedCommand中配置的此方法自动显示的,但它似乎并未自动本地化。我的理解是
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-de");
    System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;

    MainWindow构造函数的过程中,应确保框架提供的可本地化字符串应取自德语 CultureInfo-但是, Ctrl不会更改为 Strg,因此,除非我对如何为框架提供的字符串设置 CultureInfo感到误解,此方法无论如何,如果我希望所显示的快捷方式已正确本地化,则是不可行的。

    现在,我知道 KeyGesture 允许我为键盘快捷键指定一个自定义显示字符串,但不仅 RoutedCommand派生的 DoSomethingCommand类与我的所有实例(从那里我可以与加载的本地化联系)脱节。与 CommandBinding必须与XAML中的命令链接的方式相同, respective DisplayString property是只读的,因此当在运行时加载另一个本地化时将无法更改它。

    这使我可以选择手动浏览菜单树(编辑:为了清楚起见,此处没有代码,因为我不要求这样做,而且我知道如何执行此操作)和窗口的 InputBindings列表以检查哪个命令具有与之关联的任何 KeyBinding实例,并且哪些菜单项链接至这些命令中的任何一个,因此我可以手动设置各个菜单项中每个菜单项的 InputGestureText,以反射(reflect)第一个(或首选,以我想采用的任何度量标准)使用此处)键盘快捷键。每当我认为键绑定(bind)可能已更改时,都必须重复此过程。但是,对于某些本质上似乎是菜单栏GUI的基本功能的东西来说,这似乎是一个非常繁琐的解决方法,因此我坚信这不是实现此目的的“正确”方法。

    自动显示配置为适用于WPF MenuItem实例的键盘快捷键的正确方法是什么?

    编辑:我发现的所有其他问题都涉及如何使用 KeyBinding / KeyGesture实际启用 InputGestureText可视化暗示的功能,而没有说明如何在所述情况下自动链接这两个方面。我发现的唯一一个有希望的问题是 this,但是两年多来没有收到任何答复。

    最佳答案

    我将从警告开始。有可能您不仅需要可自定义的热键,还需要菜单本身。因此,在静态使用InputBindings之前,请三思。
    还有一种关于InputBindings的警告:它们暗示命令与窗口的可视树中的元素相关。有时您需要不与任何特定窗口连接的全局热键。

    上面所说的意味着您可以使用另一种方法,并通过正确路由到相应的命令来实现自己的应用程序范围的手势处理(不要忘记使用对命令的弱引用)。

    尽管如此,手势感知命令的思想是相同的。

    public class CommandWithHotkey : ICommand
    {
    public bool CanExecute(object parameter)
    {
    return true;
    }

    public void Execute(object parameter)
    {
    MessageBox.Show("It Worked!");
    }

    public KeyGesture Gesture { get; set; }

    public string GestureText
    {
    get { return Gesture.GetDisplayStringForCulture(CultureInfo.CurrentUICulture); }
    }

    public string Text { get; set; }

    public event EventHandler CanExecuteChanged;

    public CommandWithHotkey()
    {
    Text = "Execute Me";

    Gesture = new KeyGesture(Key.K, ModifierKeys.Control);
    }
    }

    简单 View 模型:
    public class ViewModel
    {
    public ICommand Command { get; set; }

    public ViewModel()
    {
    Command = new CommandWithHotkey();
    }
    }

    窗口:
    <Window x:Class="CommandsWithHotKeys.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:commandsWithHotKeys="clr-namespace:CommandsWithHotKeys"
    Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
    <commandsWithHotKeys:ViewModel/>
    </Window.DataContext>
    <Window.InputBindings>
    <KeyBinding Command="{Binding Command}" Key ="{Binding Command.Gesture.Key}" Modifiers="{Binding Command.Gesture.Modifiers}"></KeyBinding>
    </Window.InputBindings>
    <Grid>
    <Menu HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="Auto">
    <MenuItem Header="Test">
    <MenuItem InputGestureText="{Binding Command.GestureText}" Header="{Binding Command.Text}" Command="{Binding Command}">
    </MenuItem>
    </MenuItem>
    </Menu>
    </Grid>
    </Window>

    当然,您应该以某种方式从配置中加载手势信息,然后使用数据初始化命令。

    下一步是像VS中一样的按键:Ctrl + K,Ctrl + D,快速搜索给出此 SO question

    关于wpf - 如何显示菜单项的工作键盘快捷键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21972243/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com