Wpf file manager


















When Application completes its initialization work, its lifetime truly begins. The lifetime of a WPF application is marked by several events that are raised by Application to let you know when your application has started, has been activated and deactivated, and has been shut down.

Starting in the. NET Framework 3. The SplashScreen class makes it easy to display a startup window while your application is loading. The SplashScreen window is created and shown before Run is called. After Run is called and the application is initialized, the application is ready to run. This moment is signified when the Startup event is raised:. Most standalone Windows applications open a Window when they begin running.

The Startup event handler is one location from which you can do this, as demonstrated by the following code. The first Window to be instantiated in a standalone application becomes the main application window by default.

This Window object is referenced by the Application. MainWindow property. The value of the MainWindow property can be changed programmatically if a different window than the first instantiated Window should be the main window. This is shown in the following code. If you handle Startup to only open a Window or navigate to a Page , you can set the StartupUri attribute in markup instead.

The following example shows how to use the StartupUri from a standalone application to open a Window. For more information on navigation, see Navigation Overview. You need to handle the Startup event to open a Window if you need to instantiate it using a non-parameterless constructor, or you need to set its properties or subscribe to its events before showing it, or you need to process any command-line arguments that were supplied when the application was launched.

In Windows, standalone applications can be launched from either a command prompt or the desktop. In both cases, command-line arguments can be passed to the application. During application initialization, WPF retrieves the command-line arguments from the operating system and passes them to the Startup event handler via the Args property of the StartupEventArgs parameter.

You can retrieve and store the command-line arguments using code like the following. Note that because the WindowState property must be set programmatically, the main Window must be opened explicitly in code. However, they can retrieve and process query string parameters from the URLs that are used to launch them. Windows allows users to switch between applications. An application can only be switched to if it has a visible Window that a user can select.

The currently selected Window is the active window also known as the foreground window and is the Window that receives user input. The application with the active window is the active application or foreground application.

An application becomes the active application in the following circumstances:. It is launched and shows a Window. A user switches from another application by selecting a Window in the application. You can detect when an application becomes active by handling the Application. Activated event. You can detect when an application becomes inactive by handling the Application.

Deactivated event. The following code shows how to handle the Activated and Deactivated events to determine whether an application is active. A Window can also be activated and deactivated. See Window. Activated and Window. Deactivated for more information. Neither Application. Activated nor Application. Deactivated is raised for XBAPs. A user closes every Window. A user closes the main Window.

To help you manage application shutdown, Application provides the Shutdown method, the ShutdownMode property, and the SessionEnding and Exit events. Shutdown can only be called from applications that have UIPermission. Standalone WPF applications always have this permission. Most applications shut down either when all the windows are closed or when the main window is closed.

Sometimes, however, other application-specific conditions may determine when an application shuts down. You can specify the conditions under which your application will shut down by setting ShutdownMode with one of the following ShutdownMode enumeration values:. The default value of ShutdownMode is OnLastWindowClose , which means that an application automatically shuts down when the last window in the application is closed by the user. This is shown in the following example.

In this case, it is your responsibility to shut an application down by explicitly calling the Shutdown method; otherwise, your application will continue running even if all the windows are closed.

For more information, see Navigation Overview. The shutdown conditions that are described by the ShutdownMode property are specific to an application. In some cases, though, an application may shut down as a result of an external condition.

The most common external condition occurs when a user ends the Windows session by the following actions:. To detect when a Windows session ends, you can handle the SessionEnding event, as illustrated in the following example. In this example, the code inspects the ReasonSessionEnding property to determine how the Windows session is ending. It uses this value to display a confirmation message to the user. If the user does not want the session to end, the code sets Cancel to true to prevent the Windows session from ending.

When an application shuts down, it may need to perform some final processing, such as persisting application state. It is defined as an event handler in the App. Its implementation is highlighted in the App. Exit can be handled by both standalone applications and XBAPs. Applications are mostly launched by the operating system in response to a user request.

However, an application can be launched by another application to perform some specific task. When the launched application shuts down, the launching application may want to know the condition under which the launched application shut down. In these situations, Windows allows applications to return an application exit code on shutdown.

By default, WPF applications return an exit code value of 0. When you debug from Visual Studio, the application exit code is displayed in the Output window when the application shuts down, in a message that looks like the following:. You open the Output window by clicking Output on the View menu. To change the exit code, you can call the Shutdown Int32 overload, which accepts an integer argument to be the exit code:. You can detect the value of the exit code, and change it, by handling the Exit event.

For more information, see Exit. You can set the exit code in both standalone applications and XBAPs. However, the exit code value is ignored for XBAPs. Sometimes an application may shut down under abnormal conditions, such as when an unanticipated exception is thrown. In this case, the application may not have the code to detect and process the exception.

For example, you can set the Application. StartupUri property with a pack URI that references the window or page that you would like to load when an application starts. When a project with Page items is compiled, the XAML items are converted to binary format and compiled into the associated assembly. Consequently, these files can be used in the same way as typical resource files. A content file is distributed as a loose file alongside an executable assembly. Although they are not compiled into an assembly, assemblies are compiled with metadata that establishes an association with each content file.

You should use content files when your application requires a specific set of application data files that you want to be able to update without recompiling the assembly that consumes them. To add a content file to a project, an application data file must be included as a Content item.

Furthermore, because a content file is not compiled directly into the assembly, you need to set the MSBuild CopyToOutputDirectory metadata element to specify that the content file is copied to a location that is relative to the built assembly. If you want the resource to be copied to the build output folder every time a project is built, you set the CopyToOutputDirectory metadata element with the Always value.

Otherwise, you can ensure that only the newest version of the resource is copied to the build output folder by using the PreserveNewest value.

The following shows a file that is configured as a content file which is copied to the build output folder only when a new version of the resource is added to the project. When the project is built, an AssemblyAssociatedContentFileAttribute attribute is compiled into the metadata of the assembly for each content file.

The value of the AssemblyAssociatedContentFileAttribute implies the path to the content file relative to its position in the project. For example, if a content file was located in a project subfolder, the additional path information would be incorporated into the AssemblyAssociatedContentFileAttribute value. The AssemblyAssociatedContentFileAttribute value is also the value of the path to the content file in the build output folder.

To load a content file, you can call the GetContentStream method of the Application class, passing a pack URI that identifies the desired content file. GetContentStream returns a StreamResourceInfo object, which exposes the content file as a Stream and describes its content type. As an example, the following code shows how to use GetContentStream to load a Page content file and set it as the content of a Frame pageFrame.

While calling GetContentStream gives you access to the Stream , you need to perform the additional work of converting it to the type of the property that you'll be setting it with. Resource files have an explicit relationship with the assemblies that they are distributed alongside, as defined by the AssemblyAssociatedContentFileAttribute. But, there are times when you may want to establish either an implicit or non-existent relationship between an assembly and an application data file, including when:.

You want to be able to update files without recompiling the assembly that they are associated with. Your application uses large data files, such as audio and video, and you only want users to download them if they choose to. If your application is a XAML browser application XBAP that was launched from the Internet or intranet, and it requests only the set of permissions that are allowed for applications launched from those locations, loose files can only be loaded from the application's site of origin launch location.

Such files are known as site of origin files. Site of origin files are the only option for partial trust applications, although are not limited to partial trust applications. Consequently, they are only downloaded when specifically requested.

If an XAML browser application XBAP application has large media files, configuring them as site of origin files means the initial application launch is much faster, and the files are only downloaded on demand. If your site of origin files are non-existent or unknown at compile time, you need to use traditional deployment mechanisms for ensuring the required files are available at run time, including using either the XCopy command-line program or the Microsoft Windows Installer.

If you do know at compile time the files that you would like to be located at the site of origin, but still want to avoid an explicit dependency, you can add those files to an MSBuild project as None item. As with content files, you need to set the MSBuild CopyToOutputDirectory attribute to specify that the site of origin file is copied to a location that is relative to the built assembly, by specifying either the Always value or the PreserveNewest value.

In Visual Studio, you create a site of origin file by adding a file to a project and setting its Build Action to None. To load a site of origin file, you can call the GetRemoteStream method of the Application class, passing a pack URI that identifies the desired site of origin file. GetRemoteStream returns a StreamResourceInfo object, which exposes the site of origin file as a Stream and describes its content type.



0コメント

  • 1000 / 1000