2000 basic articles about WPF small pose you should know & lt; 28-33 WPF startup stories & gt;

Source: Internet
Author: User

Before starting the text, we need to introduce Sean Sexton. Software Engineer from Minnesota dual city. He maintains two blogs: 2,000 Things You shoshould Know About C # And 2,000 Things You shoshould Know About WPF. He updates an important and easy-to-Forget knowledge of WPF and C # Every day in a 150-word short language similar to Weibo. Follow's blog has been around for a while and I hope to share it with you.

In this series, I will not only translate every tip, but also add my own opinions and opinions in development. In this series, I hope that I can stick to it as well, and every day's progress can make great achievements.

This series is based on mr. Sean Sexton's english blog. Sean Sexton has all rights to copyright and Revoke rights.

 

Previous Article: <1-7>, <8-14>, <15-21>, <22-27>

 

  [Xiao JIU's school is dedicated to describing extraordinary technologies in ordinary languages. If you want to reprint it, please indicate the source: xiaojiu's school. Cnblogs.com/xfuture]

 #28 WPF Portal

For a WPF independent application created using the VS new project creation method, the entry of this program is the Main method defined in App. g. cs (automatically generated code. In the default project configuration, public static void App. Main () is used as a static method.

Generally, the entry of A. net program is in the Main function of its entry class. If a project has multiple static Main methods, you need to configure the entry class in the project properties. In the startup object drop-down box, select the class containing the Main function as the class called at startup.

  

 

  

 #29 what happened in WPF Main ()

When you use VS to create a WPF independent program, VS will automatically generate an App class inherited from System. Windows. Application, which contains the Main () static function of the portal. This is the definition:public partial class App : Application

In the Main () function, the application starts in the following three steps:

1. CreateApplicationInstance

2. CallApplication. InitializeComponentMethod to build the entire application.

3. CallApplication. RunMethod to start the application.

The Code is as follows:

  

public static void Main(){    WpfApplication.App app = new WpfApplication.App();    app.InitializeComponent();    app.Run();}

  

#30 Singleton

  

The System. Application class is a Singleton, which means that you can create a maximum of Application Instances in a WPF Application. If you create a second Application instance, the program will throw an InvalidOperationException.

You can access the Current Application class through the static property Application. Current. Or use the derived App class, App. Current.

 

 #31 create a WPF control

In WPF, you can create controls in the following two ways:

1. Programming Method: use C # code

2. Declarative: Use XAML to declare controls

For example, to start a program, you must specify the start Window.

Code method:

  

private void Application_Startup(object sender, StartupEventArgs e){    MainWindow win = new MainWindow();    win.Show();}

Xaml:

  

<Application x:Class="WpfApplication.App"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    StartupUri="MainWindow.xaml">    <Application.Resources>    </Application.Resources></Application>

The two final implementations have the same effect and the MainWindow is opened.

  

 #32 WPF Lifecycle

You can obtain and process the passed command line parameters in the functions processed by the WPF Startup Event.

The command line parameters can be found in StartupEventArgs. Args. Args is a string array of input command line parameters.

The Code is as follows:

  

private void Application_Startup(object sender, StartupEventArgs e){    foreach (string s in e.Args)    {        MessageBox.Show(string.Format("Arg: {0}", s));    }}

In fact, the general practice is to override its OnStartup method to replace its startupevent handler

protected override void OnStart(string[] args) { }

  

#33 listening for Windows sessions

When a user disables Windows or a machine, WPF can listen to the event and issue a warning. The event name isApplication. SessionEndingTriggered when the user logs out or shuts down.

You can set SessionEndingCancelEventArgs. Cancel to true to prevent logout or shutdown:

  

private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e){   MessageBoxResult res = MessageBox.Show("Exiting Windows will terminate this app.  Are you sure?", "End Session", MessageBoxButton.YesNo);   if (res == MessageBoxResult.No)   {       e.Cancel = true;   }

Note that this event is triggered when windows is stopped, not when the WPF application is disabled.

In Windows or later, if your application cancels the SessionEnding event, Windows will notify you that your application has blocked its shutdown:

  

  

 

 

Later, we will continue to explore the WPF internal mechanism. Stay tuned!

If it is helpful, click here in the lower right corner ~ (**)

 

Related Article

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.