为什么 PowerShell 显示的表单有时不显示? [英] Why does a form displayed by PowerShell sometimes not show up?

查看:75
本文介绍了为什么 PowerShell 显示的表单有时不显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 PowerShell 中创建表单(窗口)时,我通常可以使用 .ShowDialog() 来显示该表单:

When I create a form (window) in PowerShell, I can usually display the form using .ShowDialog():

$form = New-Object System.Windows.Forms.Form
$form.ShowDialog()

.Visible 在 .ShowDialog() 前后设置为 False.

.Visible is set to False before and after .ShowDialog().

但是当我执行 .Show() 时,屏幕上什么也没有显示:

But when I do a .Show() nothing is displayed on the screen:

$form.Show()

并且 .Visible 现在设置为 True(大概是因为 .Show() 使表单正式可见.)

And .Visible is now set to True (presumably because .Show() made the form officially visible.)

当我现在再次尝试 .ShowDialog() 表单时,我收到以下错误消息:

When I now try to .ShowDialog() the form again, I get the following error message:

已经可见的表单无法显示为模态对话框.在调用 showDialog 之前将表单的可见属性设置为 false."

"Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog."

但是当我按照说明再次执行 .ShowDialog() 时

But when I follow the instructions to .ShowDialog() again

$form.Visible=0
$form.ShowDialog()

结果是屏幕上什么也没有显示,PowerShell 挂起且无法恢复(ctrl-c 似乎不起作用).我认为这是因为表单以模态方式显示在我看不到它的地方(或选项卡).但为什么?

the result is that nothing is displayed on the screen and PowerShell hangs and cannot recover (ctrl-c doesn't seem to work). I assume this is because the form is being displayed modally somewhere where I cannot see it (or tab to it). But why?

表单的坐标没有改变.那么表单如何决定何时物理可见,何时不可见?

The coordinates of the form haven't changed. So how does the form decide when it is physically visible and when it isn't?

推荐答案

避免使用 PowerShell 中的 Show(),因为它需要消息泵,而 PowerShell 控制台在创建表单的线程上不提供这种功能.ShowDialog() 之所以起作用,是因为操作系统在此模式调用期间执行消息泵送.创建表单并调用 ShowDialog() 对我来说很可靠.

Avoid using Show() from PowerShell as it requires a message pump and that isn't something the PowerShell console provides on the thread that creates your form. ShowDialog() works because the OS does the message pumping during this modal call. Creating the form and calling ShowDialog() works reliably for me.

这篇关于为什么 PowerShell 显示的表单有时不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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