powershell闭合形式表达式

发布于 2025-02-05 06:29:50 字数 1500 浏览 3 评论 0原文

我敢肯定我缺少一些愚蠢的东西,但我无法理解。我有一个脚本在代码运行时会创建“请等待”弹出窗口。问题是,完成脚本后,我无法关闭弹出窗口。

以下是弹出窗口的方式:

Function CheckShuffle()
{
    #Check Selection
        If ($Global:x -eq "" -or $Global:x -eq '0')
        {
            if ( $null -eq ('System.Windows.MessageBox' -as [type]) ) 
            {
                Add-Type -AssemblyName PresentationFramework
            }
            [System.Windows.MessageBox]::Show('You must select your part first', 'Warning', 'OK')
        }
        Else
        {
        OpenSplash
        #Shuffle
        CloseSplash
        }
}

以下是弹出窗口的代码:

Function OpenSplash()
{
$Splash = New-Object system.Windows.Forms.Form
$Splash.StartPosition = 'CenterScreen'
$Splash.Size = New-Object System.Drawing.Size(350,200)


$SplashLabel = New-Object System.Windows.Forms.Label
$Splash.Controls.Add($SplashLabel)

$SplashLabel.Location = New-Object System.Drawing.Point(100,60)
$SplashLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 16, [System.Drawing.FontStyle]::Bold)
$SplashLabel.Text = "Please Wait"
$SplashLabel.AutoSize = $True
$Splash.Visible = $True
$Splash.Update()
}

我尝试了几种关闭它的方法:

Function CloseSplash()
{
$This.Parent.Close()
}

该方法关闭了主表单,而不是Splash屏幕。

Function CloseSplash()
{
$Splash.Close()
}

那是一个错误:“您无法在无值的表达式上调用方法”,

看起来Powershell忘记了$ splash是什么,但是我不知道该怎么说。

谢谢大家!

编辑:值得注意的是,如果我将代码全部放在一起而不是调用代码,则弹出窗口按预期关闭。

I'm certain I'm missing something stupid, but I can't get this figured. I have a script that creates a "Please Wait" popup while the code is running. The issue is that I can't get the popup to close after the script is done.

Here is how the popup gets called:

Function CheckShuffle()
{
    #Check Selection
        If ($Global:x -eq "" -or $Global:x -eq '0')
        {
            if ( $null -eq ('System.Windows.MessageBox' -as [type]) ) 
            {
                Add-Type -AssemblyName PresentationFramework
            }
            [System.Windows.MessageBox]::Show('You must select your part first', 'Warning', 'OK')
        }
        Else
        {
        OpenSplash
        #Shuffle
        CloseSplash
        }
}

Here is the code for the popup:

Function OpenSplash()
{
$Splash = New-Object system.Windows.Forms.Form
$Splash.StartPosition = 'CenterScreen'
$Splash.Size = New-Object System.Drawing.Size(350,200)


$SplashLabel = New-Object System.Windows.Forms.Label
$Splash.Controls.Add($SplashLabel)

$SplashLabel.Location = New-Object System.Drawing.Point(100,60)
$SplashLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 16, [System.Drawing.FontStyle]::Bold)
$SplashLabel.Text = "Please Wait"
$SplashLabel.AutoSize = $True
$Splash.Visible = $True
$Splash.Update()
}

I've tried a few ways to close it:

Function CloseSplash()
{
$This.Parent.Close()
}

This one one closes the primary form, not the splash screen.

Function CloseSplash()
{
$Splash.Close()
}

That one throws an error: "You cannot call a method on a null valued expression"

It looks like Powershell is forgetting what $Splash is, but I cannot figure out how to tell it.

Thank you all!

Edit: Worth noting, if I put the code all together instead of calling it, the popup closes as intended.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2025-02-12 06:29:50

杰罗恩(Jeroen)在评论中回答了这个问题,我看不出如何将其标记为答案,所以我在这里重新发布!

解决方案:

我需要制作$ splash global。这非常简单,我只需要说:

$Global:Splash

再次感谢Jeroen!

Jeroen answered the question in a comment, and I don't see how to mark that as the answer, so I'm reposting here!

The solution:

I needed to make $splash global. This was super easy, I just had to make it say:

$Global:Splash

Thanks again Jeroen!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文