Application in WPF, that is, closing programs in three ways __WPF

Source: Internet
Author: User

First, introduce
WPF is similar to traditional WinForm, and WPF also needs a application to govern some global behavior and operations, and only one application instance can exist in each domain (application domain). Unlike WinForm, the WPF application is made up of two parts by default: App.xaml and App.xaml.cs, separating the definition from the behavior code. Of course, this and webform are similar. XAML is strictly not a pure XML format file, it is more like a DSL (domain specific Language, domain-specific language), all of its definitions are mapped directly 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.

II. self-defined classes implement the start of a WPF application with the Main method

Add a class to the project, the code for the class is as follows, and in the project options, set this class as the startup item.
Using System;
Using System.Collections.Generic;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Windows;

Namespace Wpfapplications
{public
    partial class app:application
    {
        [STAThread]
        static void Main ()
        {
          defines the Application object as the entire application portal  
            Application app = new application ();
            Method One: Call the Run method, the parameter is the form object that starts, also is the most common method 
              Window2 win = new Window2 ();
            App. Run (win);

            Method Two: Specifies that the MainWindow property of the Application object is the startup form, and then calls the parameterless Run method  
              Window2 win = new Window2 ();  
            App. MainWindow = win; 
            Win. Show ();       
              App. Run ();  

            Method Three: Start the app by URL
              . StartupUri = new Uri ("Window2.xaml", urikind.relative); 
            App. Run ();}}}

Third, application application shutdown

Onlastwindowclose (Default): The application closes when the last form closes or calls the shutdown () method of the Application object.
Onmainwindowclose when the startup form closes or calls the shutdown () method of the Application object, the application closes. (similar to the shutdown mode for C # Windows applications)
Onexplicitshutdown the application shuts down only when the shutdown () method of the Application object is invoked.

When you change the Turn off option, you can change it directly in App.xaml:

<application x:class= "Wpfapplications.app"    xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation "    xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "    startupuri=" Window2.xaml    " Shutdownmode= "Onexplicitshutdown" >    <Application.Resources>    </Application.Resources>< /application>  

You can also make changes in the code file (App.xaml.cs), but you must note that this setting is written before the App.run () method, as follows:

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

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.