C# 关闭事件被覆盖时无法调用Close
在我的C# 关闭事件被覆盖时无法调用Close,c#,wpf,C#,Wpf,在我的main窗口构造函数中,我验证了关闭事件,因为我需要调用另一个执行某些任务的方法,例如: public MainWindow() { InitializeComponent(); Closing += (x, y) => { y.Cancel = true; _discard = true; CheckSettings(); };
main窗口
构造函数中,我验证了关闭
事件,因为我需要调用另一个执行某些任务的方法,例如:
public MainWindow()
{
InitializeComponent();
Closing += (x, y) =>
{
y.Cancel = true;
_discard = true;
CheckSettings();
};
}
public void CheckSettings(bool x)
{
if(x)
Close();
}
在关闭
行中,我得到:
窗口关闭后,无法设置可见性或调用show或showdialog
为什么??(根据您评论中的要求…)
不能从关闭事件处理程序调用Close
如果确定是否可以关闭表单的逻辑在CheckSettings
中实现:
public MainWindow()
{
InitializeComponent();
Closing += (sender, args) =>
{
args.Cancel = !CheckSettings();
...
};
}
public bool CheckSettings()
{
// Check and return true if the form can be closed
}
(根据您评论中的要求…)
不能从关闭事件处理程序调用Close
如果确定是否可以关闭表单的逻辑在CheckSettings
中实现:
public MainWindow()
{
InitializeComponent();
Closing += (sender, args) =>
{
args.Cancel = !CheckSettings();
...
};
}
public bool CheckSettings()
{
// Check and return true if the form can be closed
}
在您从事件处理程序返回(调用了
CheckSettings
)之前,您使用的UI框架可能不会评估您命名为y
并启用Cancel=true
的EventArgs
的内容
例如,如果您正在使用WPF,Close
方法最终会调用另一个名为(viaInternalClose
)的方法,在编写本文时,该方法如下所示:
private void VerifyNotClosing()
{
if (_isClosing == true)
{
throw new InvalidOperationException(SR.Get(SRID.InvalidOperationDuringClosing));
}
if (IsSourceWindowNull == false && IsCompositionTargetInvalid == true)
{
throw new InvalidOperationException(SR.Get(SRID.InvalidCompositionTarget));
}
}
第一个if
的相关位检查名为\u isClosing
的成员变量,如果表单正在关闭,则抛出异常。
调用事件处理程序后,InternalClose
方法对EventArgs的Cancel
属性的状态作出反应:
CancelEventArgs e = new CancelEventArgs(false);
try
{
// The event handler is called here
OnClosing(e);
}
catch
{
CloseWindowBeforeShow();
throw;
}
// The status of the .Cancel on the EventArgs is not checked until here
if (ShouldCloseWindow(e.Cancel))
{
CloseWindowBeforeShow();
}
else
{
_isClosing = false;
// 03/14/2006 -- hamidm
// WOSB 1560557 Dialog does not close with ESC key after it has been cancelled
//
// No need to reset DialogResult to null here since source window is null. That means
// that ShowDialog has not been called and thus no need to worry about DialogResult.
}
上述代码(来自该方法)在调用
VerifyNotClosing
之后,这就是为什么在第一个调用完成之前,后续调用Close
会引发异常。直到您从事件处理程序返回(调用检查设置
),您正在使用的UI框架可能不会评估您命名为y
并将Cancel=true
设置为on的EventArgs
的内容
例如,如果您正在使用WPF,Close
方法最终会调用另一个名为(viaInternalClose
)的方法,在编写本文时,该方法如下所示:
private void VerifyNotClosing()
{
if (_isClosing == true)
{
throw new InvalidOperationException(SR.Get(SRID.InvalidOperationDuringClosing));
}
if (IsSourceWindowNull == false && IsCompositionTargetInvalid == true)
{
throw new InvalidOperationException(SR.Get(SRID.InvalidCompositionTarget));
}
}
第一个if
的相关位检查名为\u isClosing
的成员变量,如果表单正在关闭,则抛出异常。
调用事件处理程序后,InternalClose
方法对EventArgs的Cancel
属性的状态作出反应:
CancelEventArgs e = new CancelEventArgs(false);
try
{
// The event handler is called here
OnClosing(e);
}
catch
{
CloseWindowBeforeShow();
throw;
}
// The status of the .Cancel on the EventArgs is not checked until here
if (ShouldCloseWindow(e.Cancel))
{
CloseWindowBeforeShow();
}
else
{
_isClosing = false;
// 03/14/2006 -- hamidm
// WOSB 1560557 Dialog does not close with ESC key after it has been cancelled
//
// No need to reset DialogResult to null here since source window is null. That means
// that ShowDialog has not been called and thus no need to worry about DialogResult.
}
上述代码(来自该方法)在调用
VerifyNotClosing
之后,这就是为什么在第一个调用完成之前,后续调用Close
会导致抛出异常。您不能从关闭事件处理程序调用Close
。您可能需要让CheckSettings
返回bool,使用其返回值设置y.Cancel
,并从CheckSettings
中删除Close
调用在窗口关闭时关闭窗口没有意义,如果框架没有做任何特殊的检测,也会导致无限循环。与其问“为什么它会出现错误”,不如问问自己为什么要这样做。@vc74您能否给出一个示例,说明您不能从关闭事件处理程序调用Close
。您可能需要让CheckSettings
返回bool,使用其返回值设置y.Cancel
,并从CheckSettings
中删除Close
调用在窗口关闭时关闭窗口没有意义,如果框架没有做任何特殊的检测,也会导致无限循环。不要问“为什么它会出错”,你应该问问自己为什么要这样做。@vc74你能举个例子吗