Powershell无法找到类型[System.Windows.Forms.KeyEventHandler] [英] Powershell unable to find type [System.Windows.Forms.KeyEventHandler]

查看:313
本文介绍了Powershell无法找到类型[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代码可以显示带有TextBoxes的简单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:\Scripts\GUI-Testing.ps1:161 char:1

$TestVar=[System.Windows.Forms.KeyEventHandler]
CategoryInfo          : InvalidOperation: (System.Windows.Forms.KeyEventHandler:TypeName)
FullyQualifiedErrorId : TypeNotFound

奇怪的是,如果我关闭GUI然后重新运行脚本,我不会得到无法找到类型的错误,并且

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')会出现此错误使用 1参数调用 Load的异常:无法加载文件或程序集'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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆