简体   繁体   English

如何阻止 Powershell WPF 表单关闭

[英]How to stop a Powershell WPF Form from closing

I have been trying to implement a way to stop the user from manually clicking the red 'x' on my WPF form in Powershell.我一直在尝试实现一种方法来阻止用户在 Powershell 中手动单击我的 WPF 表单上的红色“x”。 Research online indicates that the best way to do is to use a the form closing event, stop the event and hide the form instead.在线研究表明,最好的方法是使用表单关闭事件,停止事件并隐藏表单。 It seems there are many methods of doing so in C# but i cant seem to find any for Powershell.在 C# 中似乎有很多方法可以这样做,但我似乎找不到任何适用于 Powershell 的方法。 For example, something like this can be used in C#.例如,可以在 C# 中使用这样的东西。

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

is there anything equivalent I could use in Powershell?有什么可以在 Powershell 中使用的等价物吗?

The event for form closing in Powershell is add_Closing , but I cant figure out how to stop the closing of the form. Powershell 中关闭表单的事件是add_Closing ,但我不知道如何停止关闭表单。

For anyone interested, this is how simple it was:对于任何感兴趣的人,这就是它的简单:

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

Just a quick note, this will work if you are using a WPF form.只是一个简短的说明,如果您使用 WPF 表单,这将起作用。 If you are using a WinForm, you will have to use the [System.Windows.Forms.FormClosingEventHandler] to make it work.如果您使用的是 WinForm,则必须使用[System.Windows.Forms.FormClosingEventHandler]使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM