Vi. the application class for WPF

Source: Internet
Author: User

  1. Application. Shutdownmode Property: By, as long as a window is not closed, the application class keeps the application in a valid state, and if this is not the desired behavior, you can adjust the property.
  2. Application Events
    Application provides a handful of very useful events, as shown in:

    There are two options for handling events: Associating an event handler with an event attribute in XAML, or overriding the appropriate protected method, when overriding an application method, it is recommended to call the implementation of the base class first, typically the implementation of the base class simply raises the corresponding application event.
  3. Initial interface (SplashScreen)
    WPF applications run fast, but cannot be started in an instant. When you start the application for the first time, there is some delay because the common language runtime needs to initialize first. NET environment, and then start the application. This delay is not necessarily a problem, usually, it takes only a short time before the first window appears. However, if you have more time-consuming initialization steps, or if you just want to make your application look more professional by displaying an open graphic, you can use the simple initial interface features that WPF provides. Here's how to add the initial interface:
    1) Add an image file for the project.
    2) Set the build action of this image file to SplashScreen
    The next time the application is run, the graph will immediately appear in the center of the screen, and once the runtime environment is ready, and the Application_Startup method executes, the first window of the application is displayed, and the initial interface graph will quickly fade away (about 300 millimeters).
    We can write our own initial interface display logic to change the speed at which the initial interface fades, so we need to pass false to the Splashscreen.show () method and then call Splashscreen.close () Method and can provide a timespan value that indicates how long it will take to fade out of the initial interface.
  4. Handling command-line arguments
    In order to handle command-line arguments, you need to respond to the Application.Startup event, which is provided by the Startupeventargs.args property as a string array. For example, suppose you want to load a document with the name of the document passed as a command-line argument, in which case it is necessary to read the command-line arguments and perform some additional initialization operations that are required, in the following example, This pattern is implemented by responding to the Application.Startup event, where the Application.StartupUri property is not set anywhere and the main window is instantiated using code.
     Public Partial classapp:application{//The command-line argument is set through the Visual Studio//Project Properties (the Debug tab).    Private voidApp_startup (Objectsender, StartupEventArgs e) {                   //at this point, the main window has been created and not shown.FileViewer win =NewFileViewer (); if(E.args.length >0)        {            stringFile = e.args[0]; if(File.exists (File)) {//Configure the main window. win.            LoadFile (file); }        }        //This window would automatically be set as the Application.mainwindow.win.    Show (); }}
    View Code

  5. Accessing the current Application object
    With the static Application.current property, you can get the current application instance anywhere in the application.
    In a window you can examine the contents of the application.windows Collection, which provides a reference to all the currently open windows.
  6. Interact with this window
    In the application class we can add code to hold references to important windows so that one window can access another. For example, suppose you want to track all the document windows that your application uses, you can create a specialized collection in your custom application class. The following is an example of saving a set of custom window objects using a generic list collection. In this example, each document window is represented by an instance of a class named document:
     Public Partial class app:application{    privatenew list<document>();              Public List<document> documents    {        getreturn  documents;}         Set {documents = value;}}    }
    View Code

    Now, when creating a new document, just remember to add it to the Documents collection. The following is an event handler that responds to a button click event, creates a new document and adds it to the Documents collection, as well, You can also respond to events such as window.loaded in the document class to ensure that the document object is always registered in the Documents collection when it is created.

    Private void Cmdcreate_click (object  sender, RoutedEventArgs e) {    new  Document ();     this;    Doc. Show ();    (APP) (application.current). Documents.Add (DOC);}
    View Code

    The collection can now be used anywhere else in the code to traverse all documents, and in this example the document class contains a custom SetContent () method for updating the display:

    Private void Cmdupdate_click (object  sender, RoutedEventArgs e) {    foreach in (APP) (application.current). Documents)    {        doc. SetContent ("" ". " );    }            }
    View Code
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.