简体   繁体   English

MVVM中的Silverlight热键命令?

[英]Hotkey commands for Silverlight in MVVM?

I'm trying to fire commands based on keystrokes in Silverlight. 我正在尝试基于Silverlight中的击键来触发命令。 As I understand you cannot use AccessKey or AcceleratorKey in Silverlight. 据我了解,您无法在Silverlight中使用AccessKey或AcceleratorKey。 Also it looks like the might be helpful attached property InputBindings does not work either. 它看起来可能是有用的附加属性InputBindings也不起作用。

I started looking in other places. 我开始在其他地方寻找。 It looked like Prism was the way to get commands working in Silverlight, so I checked that out. 看起来Prism是让Silverlight中的命令工作的方式,所以我检查了一下。 However they only have a Click handler, which isn't even a useful starting point for getting key commands set up. 但是它们只有一个Click处理程序,它甚至不是设置关键命令的有用起点。

Am I just missing some part of Prism? 我只是错过了Prism的某些部分吗? Or is there a good standard way of handling hotkeys with MVVM Silverlight? 或者有一个很好的标准方法来处理MVVM Silverlight的热键?

It sounds like you're looking for a "codeless" MVVMish way of handling the KeyUp/KeyPress/KeyDown event. 听起来你正在寻找处理KeyUp / KeyPress / KeyDown事件的“无代码”MVVMish方式。

Option #1: Prism. 选项#1:棱镜。
You've mentioned it only ships with the Click command. 你提到它只附带 Click命令。 However, you can add your own attached DPs to enable commands for whatever event you'd like (like KeyUp/KeyDown/KeyPress). 但是,您可以添加自己的附加DP,以便为您喜欢的任何事件启用命令(如KeyUp / KeyDown / KeyPress)。

If you're looking for a sample on that Corey has a good one for ToggleButton.Checked/Unchecked events. 如果你正在寻找一个样本,那么Corey有一个很好的ToggleButton.Checked / Unchecked事件。
http://www.85turns.com/2009/06/24/togglebutton-command-for-prism/ http://www.85turns.com/2009/06/24/togglebutton-command-for-prism/

<ToggleButton x:Name="ToggleButton1" 
            customCommands:Checked.Command="{Binding CheckedCommand}"
            customCommands:UnChecked.Command="{Binding UnCheckedCommand}"
        Margin="8,8,0,8" Content="Check me"
        />

Also, Erik Mork has an excellent video that gives you a good overview on commands and how to create a custom command Attached DP. 此外,Erik Mork还有一个出色的视频,可以很好地概述命令以及如何创建自定义命令Attached DP。 http://development-guides.silverbaylabs.org/Video/Prism-Commands http://development-guides.silverbaylabs.org/Video/Prism-Commands

Option #2: Blend Triggers 选项#2:混合触发器
The Expression Blend SDK ships with Triggers and Behaviours that are spot on to what you're try to do. Expression Blend SDK附带了触发器和行为,这些触发器和行为可以指向您尝试执行的操作。
Blend Examples codeplex project ships with a EventTrigger you could use: Blend示例 codeplex项目随附一个您可以使用的EventTrigger:

<i:EventTrigger EventName="Click">
      <si:InvokeDataCommand Command="{Binding ShoppingCart.CheckOutCommand}"/>
</i:EventTrigger>

Or, you could create your own custom Trigger for Key stroke events and do there whatever you'd like. 或者,您可以为关键笔划事件创建自己的自定义触发器,并根据您的喜好进行操作。 Here's a sample: 这是一个示例:
http://azurecoding.net/blogs/brownie/archive/2009/04/06/blend-behaviors-ftw.aspx http://azurecoding.net/blogs/brownie/archive/2009/04/06/blend-behaviors-ftw.aspx

Do you mean like Ctrl+v or such I have seen the following example at the MSDN site . 你是说像Ctrl + v或类似我在MSDN网站上看到以下示例。

void Canvas_KeyUp(object sender, KeyEventArgs e)
{
    //check for the specific 'v' key, then check modifiers
    if (e.Key==Key.V) { 
        if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) {
        //specific Ctrl+V action here
        }
    } // else ignore the keystroke
}

The MVVM toolkit contains an interesting CommandReference class that allows you to bind InputBindings to ViewModel commands. MVVM工具包包含一个有趣的 CommandReference类,允许您将InputBindings绑定到ViewModel命令。 I'm not sure it works for Silverlight, but you can give it a try... 我不确定它适用于Silverlight,但你可以尝试一下......

OK, as RandomEngy pointed out, there are no InputBindings in Silverlight... 好的,正如RandomEngy指出的那样,Silverlight中没有InputBindings ......

However, I think you could use attached behaviors. 但是,我认为你可以使用附加行为。 It's a way to "bind" an event to a command of the ViewModel. 这是一种将事件“绑定”到ViewModel命令的方法。 Marlon Grech has a good implementation here Marlon Grech 在这里很好的实施

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

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