C# 如何在关闭窗口时显示MainWindow?

C# 如何在关闭窗口时显示MainWindow?,c#,wpf,C#,Wpf,当我关闭同一应用程序中的特定窗口时,我试图将我的主窗口显示在视图中。我尝试过这样做,但是用我创建的代码,它只是创建了一个新的MainWindow实例,我最终得到了两个MainWindow,而不是想要的一个。下面是我得到的代码 private void Weight_Click(object sender, RoutedEventArgs e) { try { MultipleConverters.Windows.Weight We

当我关闭同一应用程序中的特定窗口时,我试图将我的主窗口显示在视图中。我尝试过这样做,但是用我创建的代码,它只是创建了一个新的MainWindow实例,我最终得到了两个MainWindow,而不是想要的一个。下面是我得到的代码

 private void Weight_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
            WeightCalculation.Show();
            this.WindowState = WindowState.Minimized;
        }
//上面的代码工作正常,最小化了主窗口并将所选窗口带入视图

private void Quit_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        MainWindow bringIntoView = new MainWindow();
        bringIntoView.Show();
    }
//以上代码就是问题代码。这段代码在新窗口中,我试图实现的是,当此窗口关闭时,主窗口将重新进入作用域,而不是创建它的新实例,并留给我2个主窗口,而不是所需的1个主窗口。任何帮助都会很好

使用该属性存储对主窗口的引用,然后可以使用该属性将窗口恢复

private void Weight_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
        WeightCalculation.Owner = this;
        WeightCalculation.Show();
        this.WindowState = WindowState.Minimized;
    }
别处

private void Quit_Click(object sender, RoutedEventArgs e)
{
    this.Close();
    Owner.WindowState = WindowState.Normal;
}
但是,根据您显示的行为,您可能希望查看使用而不是最小化父窗口,并使用它

private void Weight_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
        WeightCalculation.Owner = this;
        WeightCalculation.ShowDialog(); //The code pauses here till the dialog is closed.
    }
使用该属性存储对主窗口的引用,然后可以使用该属性将窗口恢复

private void Weight_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
        WeightCalculation.Owner = this;
        WeightCalculation.Show();
        this.WindowState = WindowState.Minimized;
    }
别处

private void Quit_Click(object sender, RoutedEventArgs e)
{
    this.Close();
    Owner.WindowState = WindowState.Normal;
}
但是,根据您显示的行为,您可能希望查看使用而不是最小化父窗口,并使用它

private void Weight_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
        WeightCalculation.Owner = this;
        WeightCalculation.ShowDialog(); //The code pauses here till the dialog is closed.
    }
使用该属性存储对主窗口的引用,然后可以使用该属性将窗口恢复

private void Weight_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
        WeightCalculation.Owner = this;
        WeightCalculation.Show();
        this.WindowState = WindowState.Minimized;
    }
别处

private void Quit_Click(object sender, RoutedEventArgs e)
{
    this.Close();
    Owner.WindowState = WindowState.Normal;
}
但是,根据您显示的行为,您可能希望查看使用而不是最小化父窗口,并使用它

private void Weight_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
        WeightCalculation.Owner = this;
        WeightCalculation.ShowDialog(); //The code pauses here till the dialog is closed.
    }
使用该属性存储对主窗口的引用,然后可以使用该属性将窗口恢复

private void Weight_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
        WeightCalculation.Owner = this;
        WeightCalculation.Show();
        this.WindowState = WindowState.Minimized;
    }
别处

private void Quit_Click(object sender, RoutedEventArgs e)
{
    this.Close();
    Owner.WindowState = WindowState.Normal;
}
但是,根据您显示的行为,您可能希望查看使用而不是最小化父窗口,并使用它

private void Weight_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
        WeightCalculation.Owner = this;
        WeightCalculation.ShowDialog(); //The code pauses here till the dialog is closed.
    }
您正在创建MainWindow的新实例,然后显示它。这就是为什么会显示一个新的MainForm

您可以在WeightCalculation窗口上设置如下属性:

public MainWindow _mainWindow { get; set; }
在显示WeightCalculation之前,请将_mainWindow设置为mainWindow的当前实例:

MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
WeightCalculation._mainWindow = this;
WeightCalculation.Show();
this.WindowState = WindowState.Minimized;
从新表单中,您现在可以与主窗口交互

您正在创建MainWindow的新实例,然后显示它。这就是为什么会显示一个新的MainForm

您可以在WeightCalculation窗口上设置如下属性:

public MainWindow _mainWindow { get; set; }
在显示WeightCalculation之前,请将_mainWindow设置为mainWindow的当前实例:

MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
WeightCalculation._mainWindow = this;
WeightCalculation.Show();
this.WindowState = WindowState.Minimized;
从新表单中,您现在可以与主窗口交互

您正在创建MainWindow的新实例,然后显示它。这就是为什么会显示一个新的MainForm

您可以在WeightCalculation窗口上设置如下属性:

public MainWindow _mainWindow { get; set; }
在显示WeightCalculation之前,请将_mainWindow设置为mainWindow的当前实例:

MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
WeightCalculation._mainWindow = this;
WeightCalculation.Show();
this.WindowState = WindowState.Minimized;
从新表单中,您现在可以与主窗口交互

您正在创建MainWindow的新实例,然后显示它。这就是为什么会显示一个新的MainForm

您可以在WeightCalculation窗口上设置如下属性:

public MainWindow _mainWindow { get; set; }
在显示WeightCalculation之前,请将_mainWindow设置为mainWindow的当前实例:

MultipleConverters.Windows.Weight WeightCalculation = new Windows.Weight();
WeightCalculation._mainWindow = this;
WeightCalculation.Show();
this.WindowState = WindowState.Minimized;

从新表单中,您现在可以与主窗口交互。

有一个方便的属性
Application.Current.MainWindow
,您可以使用它访问App.xaml中声明的主窗口,您应该可以通过调用以下命令来显示它:

Application.Current.MainWindow.Show();
Application.Current.MainWindow.Activate();
为了简化工作,您可以在主窗口上创建一个静态方法来处理所有这些问题:

public static void TryReveal()
{
    var mainWindow = Application.Current.MainWindow;

    if (mainWindow == null)
    {
        // The main window has probably been closed.
        // This will stop .Show() and .Activate()
        // from throwing an exception if the window is closed.
        return;
    }

    if (mainWindow.WindowState == WindowState.Minimized)
    {
        mainWindow.WindowState = WindowState.Normal;
    }

    // Reveals if hidden
    mainWindow.Show();

    // Brings to foreground
    mainWindow.Activate();
}
然后您的其他窗口就可以调用
MainWindow.TryReveal()
。这样,当静态方法处理主窗口时,您的窗口不需要任何对主窗口的引用



不过,在WPF中处理这个问题的最好方法是(我认为)使用消息传递实现(例如MVVM Light的消息传递系统,或Caliburn.Micro的EventAggregator)。您的主窗口将订阅“MainWindowViewStateMessage”或类似内容(由您定义),而您的其他窗口将通过消息传递系统传递它。主窗口将拦截它并执行必要的工作。

有一个方便的属性
Application.Current.MainWindow
,您可以使用它访问App.xaml中声明的主窗口,您应该可以通过调用以下命令来显示它:

Application.Current.MainWindow.Show();
Application.Current.MainWindow.Activate();
Application.Current.MainWindow.Activate();
为了简化工作,您可以在主窗口上创建一个静态方法来处理所有这些问题:

public static void TryReveal()
{
    var mainWindow = Application.Current.MainWindow;

    if (mainWindow == null)
    {
        // The main window has probably been closed.
        // This will stop .Show() and .Activate()
        // from throwing an exception if the window is closed.
        return;
    }

    if (mainWindow.WindowState == WindowState.Minimized)
    {
        mainWindow.WindowState = WindowState.Normal;
    }

    // Reveals if hidden
    mainWindow.Show();

    // Brings to foreground
    mainWindow.Activate();
}
然后您的其他窗口就可以调用
MainWindow.TryReveal()
。这样,当静态方法处理主窗口时,您的窗口不需要任何对主窗口的引用



不过,在WPF中处理这个问题的最好方法是(我认为)使用消息传递实现(例如MVVM Light的消息传递系统,或Caliburn.Micro的EventAggregator)。您的主窗口将订阅“MainWindowViewStateMessage”或类似内容(由您定义),而您的其他窗口将通过消息传递系统传递它。主窗口将拦截它并执行必要的工作。

有一个方便的属性
Application.Current.MainWindow
,您可以使用它访问App.xaml中声明的主窗口,您应该可以通过调用以下命令来显示它:

Application.Current.MainWindow.Show();
Application.Current.MainWindow.Activate();
Application.Current.MainWindow.Activate();
为了简化工作,您可以在主窗口上创建一个静态方法来处理所有这些问题:

public static void TryReveal()
{
    var mainWindow = Application.Current.MainWindow;

    if (mainWindow == null)
    {
        // The main window has probably been closed.
        // This will stop .Show() and .Activate()
        // from throwing an exception if the window is closed.
        return;
    }

    if (mainWindow.WindowState == WindowState.Minimized)
    {
        mainWindow.WindowState = WindowState.Normal;
    }

    // Reveals if hidden
    mainWindow.Show();

    // Brings to foreground
    mainWindow.Activate();
}
然后您的其他窗口就可以调用
MainWindow.TryReveal()
。这样,当静态方法处理主窗口时,您的窗口不需要任何对主窗口的引用


不过,在WPF中处理这个问题的最好方法是(我认为)使用消息传递实现(例如MVVM Light的消息传递系统,或Caliburn.Micro的EventAggregator)。您的主窗口将订阅“MainWindowViewStateMessage”或类似于t的内容