如何在 C# 中的 WPF 中(重置 || 重新启动 || 清除)form2

How to (reset || restart || clear) form2 in WPF in C#

My application is consist of two forms.

Form1 and Form2

目前我在 form2 中遇到问题。问题是我想重新启动我的 form2。要重新启动我的 form2,我正在使用

application.restart();

但是这会重新启动整个项目并且 form1 再次出现。

My question is that is there any way so i could able to restart my only form2 not whole application.

下面是简单的代码解释。

private void btn_restart_Click(object sender, EventArgs e)
        {

            {
                Application.Restart(); //Restart whole project :(


            }
}

But this restart whole project and form1 appears again.

是的,预计方法 Restart 正在 Application class 并且其唯一目的是重新启动整个应用程序。

您可以使用 Close() 以编程方式调用关闭 window 操作,然后您可以创建您的表单的新实例,如:

private void btn_restart_Click(object sender, EventArgs e)
{

  form2.Close();  // close it
  form2 = new Form2(); // reopen it
  form2.Show(); // show on the screen

}

如果您 Form2 需要构造函数中的参数,那么您当然需要传递这些参数,假设它在回答此问题时具有无参数构造函数。

希望对您有所帮助。