First of allApplicationAbility to implement several functions, such as accessingcommand-line arguments, gets the currentexample of a program class, as well as the commonSingle-Window program instance.To Access command-line arguments:The first type: Get access using Environment.getcommandlineargs (); second: responseApplicationClass ofOnstartup ()Event. Example:
/// <summary> ///perform the corresponding action by using the class Property = = Debug + command-line arguments/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidApplication_Startup (Objectsender, StartupEventArgs e) { BOOLstartminmized =false; for(inti =0; i < e.args.length; i++) { if(E.args[i] = ="/startminmized") startminmized=true; } MainWindow win=NewMainWindow (); if(startminmized) {win. WindowState=windowstate.minimized; Win. Content="the current command parameters are:"+ e.args[0]; } win. Show (); }
get the current program instance:application.currentProperty accessApplicationThe instance of the class.ApplicationThe object provides several fairly useful properties:MainWindowDevelopers can access this property to get the main window of the application.WindowsThe collection property is used to get a collection of currently open referenced program windows.Propertiesproperty to access and set settings for the application,Resourcesproperty to access and set the resources for the application.single-instance applications (1)
/// <summary> ///the interactive logic of App.xaml/// </summary> Public Partial classapp:application {/// <summary> ///The mutex becomes the synchronization primitive in the System.Threading namespace, or becomes a mutex. ///When a reference program class is created, a system-wide named mutex object is created at the same time. ///This mutex unit is visible throughout the operating system. /// </summary>mutex mutex; protected Override voidOnstartup (StartupEventArgs e) {Base. Onstartup (e); stringMutexname ="singleinstanceapplication"; BOOLcreatednew; //determine if an application has created the same instance nameMutex =NewMutex (true, Mutexname, outcreatednew); if(!creatednew) {MessageBox.Show ("There is already a reference program Force"); Shutdown (); } } }single-instance Applications (2)Create a single process that resembles an Excel program for multiple forms programs.//Program Entry
namespace singleinstancewithcommunication{ publicclass Startup { [STAThread] Public Static void Main (string[] args) { new singleapplicationbase (); Sab. Run (args);}} }
The basic class of the singleton program, and the first ADD DLL:Microsoft.VisualBasic;using Microsoft.VisualBasic.ApplicationServices
namespacesingleinstancewithcommunicationhcommunication{ Public classsingleapplicationbase: windowsformsapplicationbase { Publicsingleapplicationbase () {//set up an application as a singleton program This. Issingleinstance =true; }//Singleinstancewithcommunication.app App Wpfapp; protected Override BOOLOnstartup (StartupEventArgs eventArgs) {Wpfapp=New App(); Wpfapp.run (); return false; } protected Override voidonstartupnextinstance (Startupnextinstanceeventargs eventArgs) {//after the current program is opened, continuing to open the new program will trigger the Onstartupnextinstance event, the program to make the corresponding action. //you can get the current parameter to manipulate the action in the Open program. Base. Onstartupnextinstance (EventArgs); if(EventArgs.CommandLine.Count >0) Wpfapp.showwindowtext (eventargs.commandline[0]); } }}AboutAPPRelated code for variables
namespacesingleinstancewithcommunication{/// <summary> ///the interactive logic of App.xaml/// </summary> Public Partial class App : application {protected Override voidOnstartup (StartupEventArgs e) {Base. Onstartup (e); MainWindow win=NewSingleinstancewithcommunication.mainwindow (); This. MainWindow =win; Win. Show (); if(E.args.length >0) Showwindowtext (e.args[0]); } Public voidShowwindowtext (stringfileName) {Window1 win=NewWindow1 (); Win. Title=FileName; ((MainWindow) This. MainWindow). LSTBOX.ITEMS.ADD (FileName); Win. Owner= This. MainWindow; Win. LoadFile (FileName); Win. Show (); } }}
Tasks for the WPF application class: