WPF使用语句打开另一个表单

Jro*_*row 3 c# wpf winforms

我在Winforms中编写了一个C#应用程序,现在我在WPF中重写它.在Winforms版本中,我使用以下内容打开另一个窗口,同时向其发送信息并从中接收信息:

using (showSelection showSelection1 = new showSelection(listBox2.SelectedItem.ToString()))
            {
                showSelection1.ShowDialog();               
                storage1.showID = showSelection1.showID;
                storage1.numOfSeasons = showSelection1.numOfSeasons;

            }
Run Code Online (Sandbox Code Playgroud)

这工作正常,我从showSelection表单中发送listBox2并接收showIDnumOfSeasons使用此代码:

this.showID = Convert.ToInt32(dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value);
this.numOfSeasons = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
this.Close();
Run Code Online (Sandbox Code Playgroud)

现在,在WPF版本中我尝试了同样的事情:

using (ShowSelection showSelection = new ShowSelection(showListBox.SelectedItem.ToString()))
            {

            }
Run Code Online (Sandbox Code Playgroud)

但在里面using( )我得到这个错误: ShowSelection: type used in a using statement must be implicitly convertible to 'System.IDisposable'

我不确定从这里做什么.我可以解决这个问题并且仍然以同样的方式解决这个问题,或者我应该采用不同的方式做到这一点吗?ShowSelection窗口只是一个带有单个按钮的数据网格.

Jcl*_*Jcl 6

WPF组件不使用Win32句柄(Window确实如此,但它是自我管理的),因此它们没有必要IDisposable,并且您不需要Dispose它们或在using块中使用它们.

一旦没有更多的引用,Window它将被GC标记为收集,与其他纯.NET组件相同.

如果你想在一个using块中使用它,你可以IDisposable手动在你的窗口上实现,但它确实不需要.

如果您想手动删除资源(并在using块中继续使用它),那么您可以做的最简单:

public class ShowSelection : Window, IDisposable
{
    public void Dispose()
    {
      /* here you'd remove any references you don't need */
    }
}
Run Code Online (Sandbox Code Playgroud)

但除非有需要,否则我建议不要这样做