如何停止Powershell WPF窗体关闭
问题描述:
我一直在试图实现一种方法来阻止用户在Powershell中的WPF窗体上手动点击红色'x'。在线研究表明,最好的方法是使用表单关闭事件,停止事件并隐藏表单。看来在C#中有很多这样做的方法,但我似乎找不到任何Powershell。例如,像这样的东西可以在C#中使用。如何停止Powershell WPF窗体关闭
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.Visibility = Visibility.Hidden;
}
是否有任何相当于我可以在Powershell中使用的东西?
在Powershell中关闭表单的事件是add_Closing
,但我不知道如何停止关闭表单。
答
任何有兴趣,这是多么简单是:
$form.add_closing
({
$_.cancel = $true
})
只是一个快速的注意,如果您使用的是WPF的形式,这将工作。如果您使用WinForm,则必须使用[System.Windows.Forms.FormClosingEventHandler]
才能使其工作。