0

I am running into a problem in a WPF/C# application while minimizing it, I have looked online and the most popular solution to Minimizing Window is to change the Mainwindow state to Minimize, and this is also what I have been doing

private void Minmize(object sender, RoutedEventArgs e)
{
  Application.Current.MainWindow.WindowState = WindowState.Minimized;
}

This works for me fine If its from a window which is initialized as the startup URI of the Application, simply put the 'Minimize' event fired will only work if the button that triggers the event "Minimize" is part of the Window that is Started when you run the Application

So In my Case there is a login Window Before, when the Application Runs the Login Window appears and Login Details are Entered, upon Successful Login This is the Code I run

private void nxtwin_Click(object sender, RoutedEventArgs e)
 {
   new MainWindow().Show();
   this.Close();
 }

SO naturally It shows MainWindow, But now the Minimize Code Doesn't Work

Might be Relevant

Now I do not know if this is related but I have My Debug bar enabled (The Black bar at the top of the application for debugging), so it initially the Debug bar loads fine, But Once I have Fired the Minimize Event, except Everything Else The Debug Bar, Minimizes!! (how do I know it minimizes and not just hide or if the visibility is changed to collapsed or hidden bcz it plays the Minimize animation before Disappearing and then never comes back )

  • Can't you just set the `WindowState` in the `MainWindow` itself (not `Application.Current.MainWindow`)? See [this question](https://stackoverflow.com/questions/2841258/minimize-a-window-in-wpf) – Xerillio May 08 '21 at 09:40
  • @Xerillio NO Because some UserControls require the Full application Real state while others don't, That's Why I just created Every Single Button for the UserControls That Require it – juliawatson May 08 '21 at 13:58
  • You can get currently instantiated MainWindow by Application.Current.Windows.OfType(). – emoacht May 08 '21 at 14:53
  • Shouldn't the code be `this.WindowState = WindowState.Minimized;` inside the `MainWindow` class? – Charlieface May 08 '21 at 23:46

0 Answers0