作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我找到了这个看起来像我需要的答案:
How can I programmatically generate keypress events in C#?
除了我无法创建 KeyEventArgs
的实例(我不知道如何创建)
有问题的代码是:
var key = Key.Insert; // Key to send
var target = Keyboard.FocusedElement; // Target element
var routedEvent = Keyboard.KeyDownEvent; // Event to send
target.RaiseEvent(
new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromVisual(target), //<--- HERE, I can't
0,
key)
{ RoutedEvent=routedEvent }
);
编译器说:
The best overloaded method match for
'System.Windows.PresentationSource.FromDependencyObject(System.Windows.DependencyObject)'
has some invalid arguments
ide 说:
参数类型 IInputElement
不可分配给参数类型 DependencyObject
在 StackOverflow 中,我发现了几个针对该答案的答案,但没有一个首先解决如何创建实例的问题。
我该怎么做?
最佳答案
呜呜呜
我找到了:必须使用Keyboard.PrimaryDevice.ActiveSource
InputManager.Current.ProcessInput(
new KeyEventArgs(Keyboard.PrimaryDevice,
Keyboard.PrimaryDevice.ActiveSource,
0, Key.Tab)
{
RoutedEvent = Keyboard.KeyDownEvent
}
);
关于c# - 如何在 WPF 中创建 KeyEventArgs 对象(与 SO 答案相关),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10820990/
我是一名优秀的程序员,十分优秀!