WPF Introductory Tutorial Series three--application Introduction (cont.)

Source: Internet
Author: User

Next to WPF introductory Tutorial series two--application Introduction, we continue to learn application

Iii. closure of WPF applications

The closure of a WPF application only stops when the application's Shutdown method is called. ShutDown is an implicit or explicit occurrence that can be set by specifying the Shutdownmode property value.

Shutdownmode Options

Onlastwindowclose (default value):

The application shuts down when the application shuts down, or when the last window closes, or when the shutdown () method of the Application object is called.

Onmainwindowclose

When the startup form closes or calls the Application object's shutdown () method, the application shuts down. (similar to the shutdown mode for C # Windows applications)

Onexplicitshutdown

The application is closed only when the shutdown () method of the Application object is called.

    1. Changes to the Shutdownmode option can be changed directly in App.xaml, as in the following code.
<application x:class= "Wpfapp1.app"             xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"                         Startupuri= "MainWindow.xaml" shutdownmode= "Onexplicitshutdown" >    <Application.Resources>            </ Application.resources></application>

2. Modify the Shutdownmode option in the code file (App.xaml.cs), but be aware that this setting is to be written before the App.run () method, as in the following code.

  App. Shutdownmode = Shutdownmode.onexplicitshutdown;    App. Run (Win);

Application other properties of the object:

Name

Description

Current

Gets the application object for the current AppDomain.

Dispatcher

Gets the Dispatcher associated with this dispatcherobject. (inherited from Dispatcherobject.) )

MainWindow

Gets or sets the main window of the application.

Properties

Gets the application-scoped collection of properties.

resourceassembly

Gets or sets a Assembly that provides a boxed Uniform Resource Identifier (URI) for a WPF application's resource.

Resources

Gets or sets the collection of application-scoped resources, such as styles and brushes.

Shutdownmode

Gets or sets the condition that causes the Shutdown method call to occur.

StartupUri

Gets or sets the UI that is automatically displayed when the application starts.

Windows

Gets the instantiation window in the application.

Iv. adding Application Object events

Name

Description

Activated

triggered when an application becomes a foreground application.

Deactivated

Triggered when the application is no longer a foreground application.

Dispatcherunhandledexception

Triggered if the exception was thrown by the application but not processed.

Exit

Triggered before the application is closed, and cannot be undone.

FragmentNavigation

Occurs when a browser in the application initiates navigation to the content fragment navigation, if the desired fragment is in the current directory, or after the source XAML catalog is loaded, if the desired fragment is in other content.

LoadCompleted

triggered when navigation to a directory loaded by a browser in the application is completed and rendered.

Navigated

triggered when navigating to content that has been found by the browser used in the application, it may not have finished loading yet.

Navigating

In a new navigation by the application, a browser request occurs.

NavigationFailed

The problem occurs when a browser of the application navigates to the requested content.

Navigationprogress

Periodically occurs in the application using browser management to provide navigation progress information for the download process.

navigationstopped

The Stoploading method that occurs in a browser is called in the application, or, if the new navigation is requested by the browser, the current navigation is in progress.

Sessionending

Occurs when the user logs off or shuts down the Windows session of the operating system.

Startup

Occurs when the Run method of the Application object is called.

There are three ways to add events to your application.

The first way:

1, in the App.xaml to do the event binding, in the App.xaml.cs file to add the event processing method

In the App.xaml file, see the specific Add method.

2. After adding the event, the App.xml file code is as follows

<application x:class= "Wpfapp1.app"             xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"             Startupuri= "MainWindow.xaml" shutdownmode= "Onexplicitshutdown" activated= "application_activated" Exit= " Application_exit ">    <Application.Resources>            </Application.Resources></Application>

3. The code in the App.xaml.cs file is as follows:

Using system;using system.collections.generic;using system.configuration;using system.data;using System.Linq;using System.threading.tasks;using System.Windows; namespace wpfapp1{//<summary>//    App.xaml Interactive logic///</summary> public    partial class App:application    {        private void application_activated (object sender, EventArgs e)        {         }         private void Application_exit (object sender, Exiteventargs e)        {         }}}    

4. After adding an event using the above method, if you press F5 to execute the application in Visual Studio, the following error "does not contain a static ' Main ' method appropriate for the entry point" is reported. This error is due to Visual Studio's modification of the original automatically generated App.xaml related definitions in the project file (*.csproj). The specific differences are as follows:

1) The definition of App.xaml in the directly-created WPF project is as follows:

<applicationdefinition include= "App.xaml" >      <Generator>MSBuild:Compile</Generator>      <SubType>Designer</SubType>    </ApplicationDefinition>


2) Visual Studio has the following configuration code for the modified App.xaml:

   <page include= "App.xaml" >      <SubType>Designer</SubType>      <generator>msbuild: Compile</generator>    </Page>

The first paragraph of the code APP.XAML in the project file is defined with the applicationdefinition tag. The second piece of code APP.XAML in the project file is defined by the page tag, which means that App.xaml is just a page.

Therefore, you only need to modify the App.xaml configuration from page to applicationdefinition in the project file.

The second way

1, as in the program class in WinForm write the main method, as in WPF, you can customize an app class to write main and other related events.

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.Windows; Namespace wpfapp1{    class app    {[STAThread]        static void Main ()        {            //Define Application object as the entire application portal             Application app = new application ();              MainWindow win = new MainWindow ();            App. Shutdownmode = Shutdownmode.onmainwindowclose;            App. MainWindow = win;            is required, otherwise the form win cannot be displayed                  . Show ();             App. Run ();            App. Activated + = app_activated;            App. Exit + = App_exit;         }  static void App_activated (object sender, EventArgs e) {     throw new NotImplementedException ();}  static void App_exit (object sender, Exiteventargs e) {     throw new NotImplementedException ();}}    }

The Third Way

    1. In the App.xaml interface, such as Location 1, enter the Exit event name, Visual Studio 2013 will pop up a menu "new event handler", double click on this menu, Visual Studio 2013 will automatically create a "Application_ Exit "event, such as position 2.

V. WPF Application life cycle

The life cycle and execution order of WPF applications are illustrated with an image on MSDN. Shows the order of the principal events for the lifetime of the window.

WPF Introductory Tutorial Series three--application Introduction (cont.)

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.