MVVM 处理 ViewModel 上所有未处理的按键
我不知道这是否是一个好的工作方式,但我需要处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您设置
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.查看MSDN
关注到“处理的概念”部分,特别是handledEventsToo部分。
Check out MSDN
Pay attention to "The Concept of Handled" section, especially the handledEventsToo part.