(WPF) .Close() 方法是否释放 window 实例?

(WPF) Does .Close() method releases the window instance?

我正在 On_Click 方法中创建一个新的 window。首先我尝试了这个;

public partial class MainWindow : Window
{
    CustomerOperations customerOperationsWindow;
    public MainWindow()
    {
        customerOperationsWindow = new CustomerOperations();
        InitializeComponent();
    }

    private void btnCustomer_Click(object sender, RoutedEventArgs e)
    {
        customerOperationsWindow.Owner = this;
        customerOperationsWindow.Show();
    }
}

它不起作用,所以每次用户单击“客户”按钮时我都开始创建 window 实例。我使用了以下代码。

 private void btnCustomer_Click(object sender, RoutedEventArgs e)
    {
        CustomerOperations customerOperationsWindow = new CustomerOperations();
        customerOperationsWindow.Owner = this;
        customerOperationsWindow.Show();
    }

在新 window 中,如果用户单击主按钮,我想导航到主按钮 window。

private void btnMain_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        this.Owner.Show();
    }

第一个问题:this.Close() 是否释放了 window 实例?

第二个问题:这个用法正确吗?

您认为最佳做法是什么?

谢谢大家

如果你想切换到父window,可以使用代码this.Owner.Activate();,如果你想关闭当前window,首先this.Owner.Activate();和然后 this.Close();.

当你输入this.Close()时,编译器到达后不执行后面的几行。当样本 window 仍然存在时,无需重新创建它

private void btnMain_Click(object sender, RoutedEventArgs e)
{
    this.Owner.Activate();
    this.Close();
}

Window.Close() 将释放实例分配的所有资源。这就是为什么关闭后无法再次显示的原因。

如果你想重用同一个 Window 实例,你应该取消关闭过程以防止内部资源的处置,并改为折叠 Window(通过将 Window.Visibility 设置为 Visibility.Collapsed - Visibility.Collapsed 也是调用 Window.Show() 之前实例化 Window 的默认值。

或者通过调用 Window.Hide()(这会将 Visibility 设置为 Visibility.Hidden)而不是 Window.Close().

来隐藏 Window

调用 Window.Show 也会将 window 的可见性设置为 Visibility.Visible
事实上,通过设置Window.Visibility显示一个WindowWindow.Show().

的异步版本。

通常,您可以使用 Window.Activate 方法在 Window 个实例之间切换。在当前为 showing/visible 的 Window 上调用 Window.Show,什么都不做。

public partial class MainWindow : Window
{
    CustomerOperations CustomerOperationsWindow { get; }

    public MainWindow()
    {
        InitializeComponent();
        this.CustomerOperationsWindow = new CustomerOperations();

        // Consider to move this logic to CustomerOperations class,
        // where you can override the OnClosing method instead of subscribing to the event
        this.CustomerOperationsWindow.Closing += CollapseWindow_OnClosing;
    }

    // Cancel close  to prevent disposal and collapse Window instead
    private void CollapseWindow_OnClosing(object sender, CancelEventArgs e)
    {
      e.Cancel = true;
      this.CustomerOperationsWindow.Visibility = Visibility.Collapsed;
      this.CustomerOperationsWindow.Owner.Activate();
    }

    private void btnCustomer_Click(object sender, RoutedEventArgs e)
    {
        this.CustomerOperationsWindow.Owner = this;

        // Calling Show will set the Visibility to Visibility.Visible
        this.CustomerOperationsWindow.Show();
    }
}

创建 Window 实例会分配非托管资源。如果这种情况经常发生,您将使垃圾收集器保持忙碌。从性能的角度来看,您可能希望避免它并更愿意重用相同的实例。
在常见情况下,这是没有必要的。但是由于 Window 暴露了一个 Hide() 方法,你可以考虑使用它来代替 Close().