MVVM 处理 ViewModel 上所有未处理的按键

发布于 2024-10-05 23:14:05 字数 1038 浏览 3 评论 0原文

我不知道这是否是一个好的工作方式,但我需要处理 ViewModel 上所有未处理的击键,因此我的想法是在 ShellView 上使用行为,将所有未处理的击键转发给 ViewModel 。

但问题是如何获取所有未处理的按键?

这是我第一次尝试捕获它们

Public Class ForwardKeyBehavior
    Inherits Behavior(Of DependencyObject)

    Protected Overrides Sub OnAttached()
        Keyboard.AddKeyDownHandler(Me.AssociatedObject, AddressOf OnKeyPressed)
        Keyboard.AddPreviewKeyDownHandler(Me.AssociatedObject, AddressOf OnPreviewKeyPressed)
        MyBase.OnAttached()
    End Sub

    Protected Overrides Sub OnDetaching()
        Keyboard.RemoveKeyDownHandler(Me.AssociatedObject, AddressOf OnKeyPressed)
        MyBase.OnDetaching()
    End Sub

    Private Sub OnPreviewKeyPressed(ByVal sender As Object, ByVal e As KeyEventArgs)

    End Sub

    Private Sub OnKeyPressed(ByVal sender As Object, ByVal e As KeyEventArgs)
        If (Not e.Handled) Then
            Trace.Write(e.Key.ToString())
        End If
    End Sub

End Class

,但似乎 e.Handled 总是错误的,所以我错过了什么即使我按下文本框中的按键

I don't know if this is a good way to work but i need to handle all unhandled keystrokes on my ViewModel so my idea was to use a Behavior on my ShellView that would relay all unhandled keystrokes to the ViewModel..

But the problem is how do i get all unhandled key presses?

Here is my first try to just catch them

Public Class ForwardKeyBehavior
    Inherits Behavior(Of DependencyObject)

    Protected Overrides Sub OnAttached()
        Keyboard.AddKeyDownHandler(Me.AssociatedObject, AddressOf OnKeyPressed)
        Keyboard.AddPreviewKeyDownHandler(Me.AssociatedObject, AddressOf OnPreviewKeyPressed)
        MyBase.OnAttached()
    End Sub

    Protected Overrides Sub OnDetaching()
        Keyboard.RemoveKeyDownHandler(Me.AssociatedObject, AddressOf OnKeyPressed)
        MyBase.OnDetaching()
    End Sub

    Private Sub OnPreviewKeyPressed(ByVal sender As Object, ByVal e As KeyEventArgs)

    End Sub

    Private Sub OnKeyPressed(ByVal sender As Object, ByVal e As KeyEventArgs)
        If (Not e.Handled) Then
            Trace.Write(e.Key.ToString())
        End If
    End Sub

End Class

But it seems that e.Handled always is false so what am i missing even if i press a key in a textbox?

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

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

发布评论

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

评论(2

你怎么敢 2024-10-12 23:14:05

您设置e.Handled = True 来告诉程序该事件已被处理并停止执行注册到该事件的任何其他函数。

例如,如果您将两个方法连接到 KeyPressed 事件,并且第一个方法设置e.Handled = True,那么第二个事件将永远不会被执行。

我猜测您真正需要做的就是确保您的 UnhandledKeyPressedEvent 在事件序列中排在最后,并且任何其他 KeyPressed 事件设置 e.Handled = True 以防止 UnhandledKeyPressedEvent 执行。

You set e.Handled = True to tell the program that the event has been handled and to stop executing any other functions that are registered to that event.

For example, if you hook up two methods to the KeyPressed event, and the first one sets e.Handled = True, then the 2nd event will never get executed.

I am guessing that all you really need to do is make sure your UnhandledKeyPressedEvent comes last in the event sequence, and that any other KeyPressed events set e.Handled = True to prevent the UnhandledKeyPressedEvent from executing.

少女净妖师 2024-10-12 23:14:05

查看MSDN

关注到“处理的概念”部分,特别是handledEventsToo部分。

Check out MSDN

Pay attention to "The Concept of Handled" section, especially the handledEventsToo part.

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