【问题标题】:Error when making CommandBinding in WPF在 WPF 中进行 CommandBinding 时出错
【发布时间】:2017-10-30 23:30:13
【问题描述】:

我想在 WPF 中为我的Menu 创建一些快捷方式。我用RoutedCommands 做了一个静态类,但我无法让它工作。它说我的标记无效,但我写的一切都很好。我得到的错误是:KeyGesture 不支持“None+D6”键和修饰符组合。我在RoutedCommands 的任何地方都没有使用过 D6。

相关的 XAML

Window x:Class="WorldResources.GlowingEarth"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:cmd="clr-namespace:WorldResources.View"/>
<Window.CommandBindings>
    <CommandBinding Command="cmd:RoutedCommands.NewMap" Executed="NewFile_Click" />
</Window.CommandBindings>
<DockPanel>
    <Menu DockPanel.Dock="Top">
        <Menu.ItemsPanel>
            <ItemsPanelTemplate>
                <DockPanel HorizontalAlignment="Stretch"></DockPanel>
            </ItemsPanelTemplate>
        </Menu.ItemsPanel>
        <MenuItem Header="File">
            <MenuItem Header="New">
                <MenuItem Header="_Map" Command="cmd:RoutedCommands.NewMap" />
        </MenuItem>
    </Menu>
</DockPanel>

这是我的RoutedCommands 课程

static class RoutedCommands
{
    public static RoutedUICommand NewMap = new RoutedUICommand(
        "New Map",
        "NewMap",
        typeof(RoutedCommands),
        new InputGestureCollection()
        {
            new KeyGesture(Key.M & Key.A, ModifierKeys.Control & ModifierKeys.Shift)
        }
        );
}

可能是什么问题?

【问题讨论】:

  • Key.M & Key.A 计算出新的键值
  • 您是否试图创建一个通过同时按下 MA 键以及这两个修饰符来调用的手势?或者 VS 恼人地称之为“和弦”,比如 Ctrl+M 后跟 Shift+M?如果是前者,则不支持开箱即用,但 according to an answer here it's doable。后者似乎可能涉及将所有键绑定绑定到一个奇异的状态机中。我敢打赌写起来会很有趣。

标签: c# wpf


【解决方案1】:

您的KeyGesture 无效。如果你想处理 CTRL+Shift + M 和 CTRL+Shift + A,试试这个:

static class RoutedCommands
{
    public static RoutedUICommand NewMap = new RoutedUICommand(
        "New Map",
        "NewMap",
        typeof(RoutedCommands),
        new InputGestureCollection()
        {
            new KeyGesture(Key.M, ModifierKeys.Control | ModifierKeys.Shift),
            new KeyGesture(Key.A, ModifierKeys.Control | ModifierKeys.Shift)
        }
        );
}

【讨论】:

    猜你喜欢
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多