Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WPF can't close window and run another?

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
I am having problems getting my application to work switching between the login window and the application window

When it was a windows form app, all worked fine..

WinForm Program Class
Code:
static class Program
    {

        // add the user to the system.
        public static HLPUser HLP_User = new HLPUser("Financial_Promotions");

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // check if logged in, else make user login
            if(! HLP_User.Is_Logged)
            {
                Application.Run(new Login(HLP_User));
                if (HLP_User.Is_Logged)
                {
                    Application.Run(new Financial_Promotions());
                }
            }
            else
            {
                Application.Run(new Financial_Promotions());
            }
            
            // logout user
            HLP_User.LogoutUser();

        }
    }

Now that I have ported it to WPF...
Code:
    public partial class App : Application
    {
        // add the user to the system.
        public static HLPUser HLP_User = new HLPUser("Financial_Promotions");

        [STAThread]
        static void Main()
        {
            Application app = new App();

            // check if logged in, else make user login
            if (!HLP_User.Is_Logged)
            {

                app.Run(new Login(HLP_User));

                if (HLP_User.Is_Logged)
                {
                    app.Run(new Financial_Promotions());
                }
            }
            else
            {
               app.Run(new Financial_Promotions());
            }

            // logout user
            HLP_User.LogoutUser();

        }

    }
It keeps erroring every time the login window is closed with..
'System.InvalidOperationException' {"The Application object is being shut down."}
This is being flagged up in the Financial_Promotions Window class contructor at the point of initialisation (highlighted below).
Code:
public Financial_Promotions()
        {
            [highlight #FCE94F]InitializeComponent();[/highlight]
        }
How do I refactor the code correctly for WPF?

Thanks
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
OK, after much soul searching (and Google ;-) ), I finally found a resolve.
Code:
public partial class App : Application
    {
        // add the user to the system.
        public static HLPUser HLP_User = new HLPUser("FinPro");

        [STAThread]
        static void Main()
        {
            Financial_Promotions.App app = new Financial_Promotions.App();
            app.InitializeComponent();
            app.Run();

            // logout user
            HLP_User.LogoutUser();
        }


        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // check if logged in, else make user login
            if (!HLP_User.Is_Logged)
            {

                this.MainWindow = new Login(HLP_User);
                this.MainWindow.ShowDialog();

                if (HLP_User.Is_Logged)
                {
                    this.MainWindow = new Financial_Promotions_Main();
                    this.MainWindow.Show();
                }
                else
                {
                    this.Shutdown();
                }
            }
            else
            {
                this.MainWindow = new Financial_Promotions_Main();
                this.MainWindow.Show();
            }


        }

    }
Code:
<Application x:Class="Financial_Promotions.App"
             xmlns="[URL unfurl="true"]http://schemas.microsoft.com/winfx/2006/xaml/presentation"[/URL]
             xmlns:x="[URL unfurl="true"]http://schemas.microsoft.com/winfx/2006/xaml"[/URL]
             ShutdownMode="OnExplicitShutdown"
             Startup="Application_Startup"
             >
</Application>

The key points I believe are..

1. You cannot run more than one app as part of the master program (like you can with Win Forms)
2. You must ensure the you switch off the app closing when all forms are closed (ShutdownMode)
3. The pre-main application window must be shown as a dialog to pause code execution.
4. Ensure you set the startup to the method handler (Application_Startup)
5. Ensure you set the App.xaml 'Build Action' to 'Page and the App.xaml.cs to 'Compile'
6. Use an empty app.Run() not the form you want to start with

That was a real headache and a lot of wasted time to get to the same point I was prior to going WPF.

Hey ho, I got there in the end!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top