
Namespace: System.Windows
Library: PresentationFramework
All WPF application needs to have an object of this Class. The usual way for its creation are two files like at our previous samples:
The MyApp.xaml file:
<Application xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" x:Class="DMU.MyApp" Startup="AppStartingUp" > </Application>
Like we said, AppStartingUp is our name (could be any) for the override of the event Startup that occurs when the executable (the .EXE file of the compiled application) starts - we can think like it was the Main of the usual programs.
And the MyApp.xaml.cs file:
using System;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
namespace DMU{
public partial class MyApp : Application{
void AppStartingUp(object sender, StartupEventArgs e){
//Creating an object of the subclass Window1 created at another files...
Window1 mainWindow = new Window1();
mainWindow.Show();
}
}
}
Some properties:
Current
Sometimes, inside any of the files of the application we need to use the object of the Class. When we launch an application, the system creates an Application object for our application and assigns it to the Current property. This piece of code shows how to retrieve an Application object.
Application thisApp = Application.Current;
MainWindow
Gets or sets the Window object for the application's main window.
Properties
The object Application has a collection (something like a Dictionary...), whose name is Properties, that we can access from any place in the application.
We "write" a value at this collection using, at some place of the application:
Application thisApp = Application.Current; thisApp.Properties["TextFromPage1"] = "This is a text";
We are defining a key; here:TextFromPage1.
The value can be a data typed inside a TextBox, by example. Captured like we will see at the lesson about the Class: TextBox.
And we can capture this value at another part of the application using:
Application ourApp = Application.Current; String somevalue = (String) our.Properties["TextFromPage1"];
TIP: Look how we did the conversion to string here. Sometimes this works...
And now, some events that can be "overrided" like we did at the sample at the beginning of this lesson for Startup:
Exit
Occurs when the application is about to shut down.
LoadCompleted
Startup
At the MyApp.xaml file we assign our name for any of these events, that will be overrided inside the MyApp.xaml.cs file.
REMEMBER: That you need to include the library PresentationFramework at the .CSPROJ file.
PREVIOUS LESSON NEXT LESSON T.CONTENTS HOMEPAGE
