【问题标题】:Powershell unable to find type [System.Windows.Forms.KeyEventHandler]Powershell 找不到类型 [System.Windows.Forms.KeyEventHandler]
【发布时间】:2015-01-06 03:46:29
【问题描述】:

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

我有一些 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.assembly]::Load('System.Windows.Forms.KeyEventHandler'),它给出了这个错误Exception calling "Load" with "1" argument(s): "Could not load file or assembly 'System.Windows.Forms.KeyEventHandler' or one of its dependencies. [FileNotFoundException]

【问题讨论】:

  • 我应该已经用[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral')加载这个

标签: powershell user-interface .net-assembly


【解决方案1】:

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

[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
    }
})

【讨论】:

  • 谢谢,我正在关闭其他示例,并且程序集正在 Enter 按键变量下加载。有道理powershell会抱怨事情。现在powershell很高兴!谢谢!
  • 为什么不 add-type -AssemblyName "System.Windows.Forms" ?
猜你喜欢
  • 2020-04-02
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多