首页 文章

如何在关闭窗口时将MainWindow置于视图中?

提问于
浏览
0

当我关闭同一个应用程序中的特定窗口时,我正试图将我的MainWindow带入视图 . 我试图这样做但是使用我创建的代码它只是创建一个MainWindow的新实例,我最终得到2个MainWindows而不是所需的 . 以下是我的代码 .

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个Mainwindows而不是所需的1 Mainwindow . 任何帮助都会很棒 .

4 回答

  • 1

    使用Owner属性存储对主窗口的引用,然后可以使用该属性重新启动窗口 .

    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;
    }
    

    但是,根据您显示的行为,您可能希望查看使用ShowDialog()而不是最小化父窗口并使用它 .

    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.
        }
    
  • 0
    Application.Current.MainWindow.Activate();
    
  • 1

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

    Application.Current.MainWindow.Show();
    Application.Current.MainWindow.Activate();
    

    为了简化操作,您可以在MainWindow上创建一个静态方法来处理所有这些:

    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) . 您的MainWindow将订阅“MainWindowViewStateMessage”或类似的东西(由您定义),您的其他窗口将通过消息传递系统传递它 . 主窗口将拦截它并进行必要的工作 .

  • 1
    private void Quit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            MainWindow bringIntoView = new MainWindow();
            bringIntoView.Show();
        }
    

    您正在创建MainWindow的新实例,然后显示它 . 这就是显示新MainForm的原因 .

    您可以做的一件事是在WeightCalculation窗口上设置一个属性,如下所示:

    public MainWindow _mainWindow { get; set; }
    

    在显示WeightCaculation之前,将_mainWindow设置为您当前的MainWindow实例:

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

    从新表单中,您现在可以与MainWindow进行交互 .

相关问题