WPF Study Notes-1. Application

Source: Internet
Author: User

Similar to winform, WPF also requires an application to control global behaviors and operations, and each domain can have only one
The application instance exists. Unlike winform, WPF application consists of two parts by default: App. XAML and
App. XAML. CS, which is similar to Delphi Form and separates definitions from behavior codes. Of course, webform also adopts a similar method. XAML
Strictly speaking, it is not a pure XML file. It is more like a DSL, and all its definitions are directly mapped to some code, but the specific translation work is completed by the compiler.

The following is a simple app definition.

Public partial class app: Application
{
}

When you see in the automatically generated project codeParitialYou should subconsciously look for "this code was generated by a tool ."...... However, the storage location of the automatically generated code is even more odd --OBJ/debug/APP. G. CS.

Public partial class app: system. Windows. Application
{
[Debuggernonusercode]
Public void initializecomponent ()
{
This. startupuri = new system. Uri ("window1.xaml", system. urikind. Relative );
}

[Stathread]
[Debuggernonusercode]
Public static void main ()
{
App = new app ();
App. initializecomponent ();
App. Run ();
}
}

App. startupuri is used to set mainwindow, app. Run () to start the message loop. Of course, there is also the stathread, which means that WPF still uses a UI thread to process the UI message.

We can discard the automatically generated code and manually write an app.

Public class app: Application
{
[Stathread]
Private Static void main ()
{
VaR APP = new app ();
VaR window = new window {Title = "WPF "};

App. Run (window );
}
}

Application provides some practical attributes and methods.

Current: Gets the default application instance in the domain.
Mainwindow: Get the main window instance.
Windows: Obtain all the window instances that have been instantiated.
Shutdownmode: Specifies the application. Shutdown mode, including closing the main form, closing the last window, and manually calling Shutdown ().
Properties: A thread-safe global dictionary that can store a public information.

Shutdown: This method terminates application process and returns an exit code to the operating system.

We can still use mutex to prevent multiple instances from running.

Private void application_startup (Object sender, startupeventargs E)
{
VaR creatednew = false;
VaR name = assembly. getentryassembly (). fullname;
New mutex (true, name, out creatednew );

If (! Creatednew)
{
MessageBox. Show ("there is already an instance running, exit! ");
Application. Current. Shutdown ();
}
}

Of course, you can also use the Windows attribute to determine whether the form already exists.

Private void button#click (Object sender, routedeventargs E)
{
VaR window2 = application. Current. Windows. oftype <WINDOW> (). firstordefault (W => W is window2 );
If (window2 = NULL) window2 = new window2 ();
Window2.show ();
Window2.activate ();
}

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.