Powershell 无法找到类型 [System.Windows.Forms.KeyEventHandler] [英] Powershell unable to find type [System.Windows.Forms.KeyEventHandler]
问题描述
这可能是一个非常简单的问题,但我完全迷失了并且寻找答案没有帮助.
This might be a pretty simple question but I'm totally lost and searching for an answer hasn't been helpful.
我有一些 powershell 代码来显示带有文本框的简单 GUI.某些文本框允许用户按 Enter 键来运行 Button_Click 代码.当我尝试运行 PS1 脚本时,出现以下错误:
I've got some powershell code to display a simple GUI with TextBoxes. Some of the textboxes allows the user to press Enter to run Button_Click code. When I try running the PS1 script, I get errors saying the following:
Unable to find type [System.Windows.Forms.KeyEventHandler].
Make sure that the assembly that contains this type is loaded.At C:ScriptsGUI-Testing.ps1:161 char:1
$TestVar=[System.Windows.Forms.KeyEventHandler]
CategoryInfo : InvalidOperation: (System.Windows.Forms.KeyEventHandler:TypeName)
FullyQualifiedErrorId : TypeNotFound
奇怪的部分,如果我关闭 GUI 然后重新运行脚本,我不会收到 Unable to find type
错误并且按 Enter 可以正常工作.
The strange part, if I close the GUI then re-run the script, I don't get the Unable to find type
error and pressing Enter works as desired.
以为我有答案了,我尝试使用 [void][reflection.assembly]::Load('System.Windows.Forms.KeyEventHandler')
这会导致此错误 Exception 调用带有1"参数的加载":无法加载文件或程序集System.Windows.Forms.KeyEventHandler"或其依赖项之一.[FileNotFoundException]
Thinking I had an answer, I tried using [void][reflection.assembly]::Load('System.Windows.Forms.KeyEventHandler')
which give this error Exception calling "Load" with "1" argument(s): "Could not load file or assembly 'System.Windows.Forms.KeyEventHandler' or one of its dependencies. [FileNotFoundException]
推荐答案
确保在脚本顶部加载以下程序集:
Make sure you load the following assemblies at the top of your script:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
如果它仍然无法正常工作,您可以执行以下操作:
If it's still not working, you could do something like:
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Enter")
{
#Write Code Here
}
})
这篇关于Powershell 无法找到类型 [System.Windows.Forms.KeyEventHandler]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!