×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

WPF can't close window and run another?

WPF can't close window and run another?

WPF can't close window and run another?

(OP)
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..

Quote:

'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()
        {
            InitializeComponent();
        } 
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

RE: WPF can't close window and run another?

(OP)
OK, after much soul searching (and Google winky smile ), 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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close