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