我不知道这是否是一种好的工作方式,但我需要处理我的 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 总是错误的,所以即使我在文本框中按下一个键,我还缺少什么?