Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关闭事件被覆盖时无法调用Close_C#_Wpf - Fatal编程技术网

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
方法最终会调用另一个名为(via
InternalClose
)的方法,在编写本文时,该方法如下所示:

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
方法最终会调用另一个名为(via
InternalClose
)的方法,在编写本文时,该方法如下所示:

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你能举个例子吗