Powershell 无法找到类型 [System.Windows.Forms.KeyEventHandler]

标签 powershell user-interface .net-assembly

这可能是一个非常简单的问题,但我完全迷失了方向,并且寻找答案并没有帮助。

我有一些 powershell 代码来显示带有文本框的简单 GUI。某些文本框允许用户按 Enter 键来运行 Button_Click 代码。当我尝试运行 PS1 脚本时,出现以下错误:

Unable to find type [System.Windows.Forms.KeyEventHandler].
Make sure that the assembly that contains this type is loaded.At C:\Scripts\GUI-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 键可以按需要工作。

以为我有答案,我尝试使用 [void][reflection.assemble]::Load('System.Windows.Forms.KeyEventHandler') ,它给出了此错误 Exception waiting使用“1”个参数“加载”:“无法加载文件或程序集“System.Windows.Forms.KeyEventHandler”或其依赖项之一。[FileNotFoundException]

最佳答案

确保在脚本顶部加载以下程序集:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

如果仍然不起作用,您可以执行以下操作:

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") 
    {    
        #Write Code Here
    }
})

关于Powershell 无法找到类型 [System.Windows.Forms.KeyEventHandler],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27791783/

相关文章:

arrays - 在对象中创建对象数组-Powershell

regex - Powershell将正则表达式值保留在变量中,然后构建字符串

powershell - 如何替换PowerShell脚本中程序集版本的第三个位置

c# - HRESULT 异常 : 0x80070057 (E_INVALIDARG)

.net - 加载 powershell 脚本大约 10000 次

c# - 从 C# 执行脚本时如何处理 PowerShell 无法找到导入 (AzureAd)

javascript - JavaScript 测试的 Jenkins 测试覆盖率报告

c++ - wxWidgets 中的嵌套自定义控件

facebook - Facebook 应用程序安全 Canvas URL

c# - 加载以编程方式编译的程序集失败