WPF Portal Application

Source: Internet
Author: User

1. WPF is similar to traditional WinForm, and WPF also requires a application to govern some global behavior and operations, and only one application instance exists per domain (application domain). Unlike WinForm, WPF application consists of two parts: App.xaml and App.xaml.cs, which separate the definition from the behavior code. Of course, this is more similar to WebForm. XAML is strictly not a purely XML format file, it is more like a DSL (domain specific Language, domain-specific language), all of its definitions are directly mapped to some code, but the specific translation work is given to the compiler to complete. WPF applications are managed by the System.Windows.Application class.

Three ways to import files:

defining the Application object as the entire application Portal Application app=Newapplication (); //method One: Call the Run method, the parameter is the starting Form object, is also the most common methodWindow2 win =NewWindow2 (); App.            Run (Win); //method Two: Specify the MainWindow property of the Application object as the startup form, and then call the Run method without parametersWindow2 win =NewWindow2 (); App. MainWindow=win; Win.                     Show (); App.              Run (); Method Three: Launch the app via URL. StartupUri=NewUri ("Window2.xaml", urikind.relative); App. Run ();

You can also configure in XAML: StartupUri and Shutdownmode to control the app open page and close call method:

<Applicationx:class= "Wpfapplications.app"xmlns= "Http://schemas.microsoft.com/winfx/2015/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2015/xaml"StartupUri= "Window2.xaml"Shutdownmode= "Onexplicitshutdown">    <application.resources>    </application.resources></Application> 

2, code analysis, rewrite onstartup:

1Mutex mutex=NULL;//prevent EXE multi-open2 protected Override voidOnstartup (StartupEventArgs e)3         {4Application.Current.ShutdownMode =Shutdownmode.onexplicitshutdown;5              This. Dispatcherunhandledexception + =NewSystem.Windows.Threading.DispatcherUnhandledExceptionEventHandler (app_dispatcherunhandledexception);6AppDomain.CurrentDomain.UnhandledException + =NewUnhandledexceptioneventhandler (currentdomain_unhandledexception);7             //directory where the program was taken8             stringAppPath =AppDomain.CurrentDomain.BaseDirectory;9Login2 loginWinT3 =NewLogin2 ();Ten             BOOL? Loginresult =Loginwint3.showdialog (); One  A             if(Loginresult.hasvalue = =true) && (Loginresult.value = =true)) -             { -                 //base. Onstartup (e); the                 //Application.Current.ShutdownMode = shutdownmode.onmainwindowclose; -                 BOOLCreatednew =false; -Mutex =NewMutex (true,"Wpfstart", outcreatednew); -                 if(!creatednew) +                 { -MessageBox.Show ("The program has started! ","", Messageboxbutton.ok, messageboximage.warning); +                      This. Shutdown (); A                 } at             } -             Else -             { -                  This. Shutdown (); -             } -}

Savor this code carefully and you'll find that it's really good;

1, think of preventing multiple open exe processing method;

2, think of ShowDialog (), pop-up form processing page data interaction;

3, think of the application domain and application vector (application) unhandled exception processing situation;

4, consider rewriting application's Onstartup method, deal with the business logic, really good code;

WPF Portal Application

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.