【问题标题】:How to stop a Powershell WPF Form from closing如何阻止 Powershell WPF 表单关闭
【发布时间】:2021-04-14 07:33:37
【问题描述】:

我一直在尝试实现一种方法来阻止用户在 Powershell 中手动单击我的 WPF 表单上的红色“x”。在线研究表明,最好的方法是使用表单关闭事件,停止事件并隐藏表单。在 C# 中似乎有很多这样做的方法,但我似乎无法为 Powershell 找到任何方法。例如,这样的东西可以在 C# 中使用。

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        this.Visibility = Visibility.Hidden;
    } 

我可以在 Powershell 中使用任何等效的东西吗?

Powershell 中关闭表单的事件是add_Closing,但我不知道如何停止关闭表单。

【问题讨论】:

    标签: wpf forms powershell


    【解决方案1】:

    对于任何感兴趣的人来说,这很简单:

     $form.add_closing
        ({
            $_.cancel = $true 
        })
    

    只是一个简短的说明,如果您使用的是 WPF 表单,这将起作用。如果您使用的是 WinForm,则必须使用 [System.Windows.Forms.FormClosingEventHandler] 才能使其工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 2021-09-11
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多