MVVM 中 Silverlight 的热键命令?

发布于 2024-08-06 16:45:12 字数 294 浏览 6 评论 0原文

我正在尝试根据 Silverlight 中的击键来触发命令。据我了解,您不能在 Silverlight 中使用 AccessKey 或 AcceleratorKey。另外,看起来可能有用的附加属性 InputBindings 也不起作用。

我开始寻找其他地方。看起来 Prism 是在 Silverlight 中运行命令的方法,所以我检查了一下。然而,它们只有一个 Click 处理程序,这甚至不是设置键盘命令的有用起点。

我只是缺少 Prism 的某些部分吗?或者是否有一个很好的标准方法来使用 MVVM Silverlight 处理热键?

I'm trying to fire commands based on keystrokes in Silverlight. As I understand you cannot use AccessKey or AcceleratorKey in Silverlight. Also it looks like the might be helpful attached property InputBindings does not work either.

I started looking in other places. It looked like Prism was the way to get commands working in Silverlight, so I checked that out. However they only have a Click handler, which isn't even a useful starting point for getting key commands set up.

Am I just missing some part of Prism? Or is there a good standard way of handling hotkeys with MVVM Silverlight?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光病人 2024-08-13 16:45:12

听起来您正在寻找一种“无代码”MVVMish 方式来处理 KeyUp/KeyPress/KeyDown 事件。

选项#1:棱镜。
您提到过它仅随“Click”命令一起提供。但是,您可以添加自己的附加 DP 来启用您想要的任何事件的命令(例如 KeyUp/KeyDown/KeyPress)。

如果您正在寻找有关 Corey 的示例,那么 Corey 有一个很好的 ToggleButton.Checked/Unchecked 事件示例。
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"
        />

另外,Erik Mork 有一个精彩的视频,可以让您很好地了解命令以及如何创建自定义命令 Attached DP。
http://development-guides.silverbaylabs.org/Video/Prism-Commands

选项#2:混合触发器
Expression Blend SDK 附带了适合您尝试执行的操作的触发器和行为。
混合示例 codeplex 项目附带了您可以使用的 EventTrigger:

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

或者,您可以为击键创建自己的自定义触发器活动并在那里做任何您想做的事。这是一个示例:
http://azurecoding.net/blogs/brownie /archive/2009/04/06/blend-behaviors-ftw.aspx

It sounds like you're looking for a "codeless" MVVMish way of handling the KeyUp/KeyPress/KeyDown event.

Option #1: Prism.
You've mentioned it only ships with the Click command. However, you can add your own attached DPs to enable commands for whatever event you'd like (like KeyUp/KeyDown/KeyPress).

If you're looking for a sample on that Corey has a good one for ToggleButton.Checked/Unchecked events.
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.
http://development-guides.silverbaylabs.org/Video/Prism-Commands

Option #2: Blend Triggers
The Expression Blend SDK ships with Triggers and Behaviours that are spot on to what you're try to do.
Blend Examples codeplex project ships with a EventTrigger you could use:

<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

最美的太阳 2024-08-13 16:45:12

你的意思是像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
}

Do you mean like Ctrl+v or such
I have seen the following example at the MSDN site.

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
}
甜中书 2024-08-13 16:45:12

MVVM 工具包包含一个有趣的 CommandReference 类,它允许您将 InputBindings 绑定到 ViewModel 命令。我不确定它是否适用于 Silverlight,但您可以尝试一下...

好的,正如 RandomEngy 指出的那样,Silverlight 中没有 InputBindings...

但是,我认为您可以使用附加行为。这是一种将事件“绑定”到 ViewModel 的命令的方法。 Marlon Grech 这里有一个很好的实现

The MVVM toolkit contains an interesting CommandReference class that allows you to bind InputBindings to ViewModel commands. I'm not sure it works for Silverlight, but you can give it a try...

OK, as RandomEngy pointed out, there are no InputBindings in Silverlight...

However, I think you could use attached behaviors. It's a way to "bind" an event to a command of the ViewModel. Marlon Grech has a good implementation here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文