Methods in WPF to start the main program after logging in first

Source: Internet
Author: User

"Reprint" http://blog.csdn.net/swarb/article/details/17301167

methods in WPF to start the main program after logging in first

I think it is a classic problem to start the application program after logging in, basically, if you want to write an application, it will be used in a small part. I struggled on this issue most of the genius to find a solution, my implementation method I feel a bit not authentic, if there is any master know better method Welcome Message Guide!!

First of all, the traditional C # implementation in WinForm, basically in the main function according to the first startup window DialogResult to determine whether the instance of the second window, DialogResult is in the first window based on the login situation to set. The code is as follows

Main function Code:

StaticClass Program

{

[STAThread]

Staticvoid Main ()

{

Try

{

Application.enablevisualstyles ();

Application.setcompatibletextrenderingdefault (FALSE);

Frmlogin frm =New Frmlogin ();Login

if (frm. ShowDialog () = = DialogResult.OK)

{

Application.Run (New Frmmain ());Main form

}

Else

{

Application.exit ();

}

}

catch (exception ex)

         {

             MessageBox.Show (ex. Message, "System Login", MessageBoxButtons.OK, Messageboxicon.stop);

             Application.exit ();

        }

   }< BR style= "margin:0px; padding:0px ">
}

The first login window sets the code for DialogResult:

This. DialogResult = DialogResult.OK;

This. Close ();

From:http://www.itc8.cn/a/bianchengleyuan/2011/0126/216.html

Refer to this basic idea in Winfrom, implemented in WPF. I rewrote the Onstartup function:

ProtectedOverridevoid Onstartup (StartupEventArgs e)

{

Base. Onstartup (e);



Login Loginwindow =New Login ();

Loginwindow.showdialog ();

result = Loginwindow.dialogresult;

Username = Loginwindow.username;

if (true = = result)

{

MainWindow main =New MainWindow (username);

Main. ShowDialog ();

}

} //One thing to note here is that the ShowDialog () is not directly called show (), and show () will not return any results, only with ShowDialog () to channel DialogResult

Anyway, you can only start the first window to log in, and then the second window cannot be rendered. After debugging, it is found that the main window has been instantiated but ShowDialog () does not seem to work at all. Tangled up.

Possible causes of this problem are:

1. MainWindow that's the second main window I'm going to create. No instantiation succeeded;

2. DialogResult error results in no instance of MainWindow and display process;

3. The first window is haunting.

After debugging the exclusion of the first to second possible, then it must be the first window is not set or where the problem. Google took a look at other people's WPF methods, found that there is an article mentioned in the need to set the WPF Shutdownmode to Onexplicitshutdown, try it out, it really takes effect! Simply modify the application Shutdownmode in main main function and modify the code as follows

Publicstatic void Main ()

         {

             PFM. Gui. App.app App = new PFM. Gui. App.app ();

             app. Shutdownmode = Shutdownmode.onexplicitshutdown;

             app. Run ();

        

Let's see where this shutdownmode is sacred. Shutdownmode can be set to a Shutdownmode enumeration value, including three kinds: Onlastwindowclose, onmainwindowclose, Onexplicitshutdown, The default value is Onlastwindowclose. If Shutdownmode is set to Onlastwindowclose, Windows Presentation Foundation (WPF) implicitly calls Shutdown when the last window in the application closes. This is true even if any window that is currently instantiated is set to the main window (MSDN:HTTP://GOO.GL/FAYRD). The last sentence is the key: even if any of the currently instantiated windows are set to the main window. The reference to the first Window object that is instantiated in application is automatically set to MainWindow because of the default in WPF.

That is, when we instantiate the first window, it is considered to be a mainwindow, so when it is closed, WPF has implicitly called shutdown so it is futile to instantiate the window afterwards. This is why the Shutdownmode is set to show off so that the above problem can be avoided. It is important to remember to call App.shutdown () in Onstartup to close application.

Methods in WPF to start the main program after logging in first

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.