
OK. Let's go to do "something real". We will create that basic application: the words "HELLO WORLD!" printed inside a window.
Like we said in the previous lesson:at the new Microsoft WPF (Windows Presentation Fundation) architecture a minimum application is the extentions of two Classes of the API available:
Application
Window
WPF has an interesting aproach for the creation of a subclass. It will be created using two files: an .XAML file having many tags and a .XAML.CS having the usual mode of coding.
We will write first the two files to create a subclass of the Class Window having an object of the Class Canvas (a kind of panel) and an object of the Class TextBlock that will have the "HELLO WORLD!".
Open your Notepad and type the : Window1.xaml file:
<Window
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
x:Class="DMU.Window1"
Title="DMU Tutorials Serie"
Top="100"
Left= "100"
Width="400"
Height="400"
Loaded="OnLoaded"
>
<Canvas Width="400" Height="400" Background="DeepSkyBlue">
<TextBlock Name="Text1" Canvas.Top="100" Canvas.Left="100" FontSize="20" Width="900" Height="50"></TextBlock>
</Canvas>
</Window>
Save this file on a new folder having the name (can be any): Ba1
What are we doing?
The name of the file is the name of the subclass: Window1.
The expression x:Class="DMU.Window1" means that the namespace of the subclass will be:DMU
We are defining some values for the properties Title, Top (position of the window in the screen) etc. for Window, Canvas and TextBlock.
And we have for Window an event beeing "overriding". An event is like a method that runs when "something" occurs. By example: the user press some keyboard key, or clicks a button in the window, or the window is loaded to the screen - thatīs our case here.
An event has an "official" name (here is Loaded) and we need to define a new name for the overrided version we will create (here we choose OnLoaded). The lines of code of the overrided version will be at the .XAML.CS file.
Look our Window1.xaml.cs file:
//This is a list of commonly used namespaces for a window. Not all used here.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
namespace DMU{
public partial class Window1 : Window{
public Window1(){
InitializeComponent();
}
private void OnLoaded(object sender, EventArgs e){
Text1.Text = "HELLO WORLD!";
}
}
}
At the beginning of the file we are declaring the namespace of Window: System.Windows (the library is PresentationFramework.dll and will be declared at a .CSPROJ file we will see soon).
The namespace of the Class Canvas is:System.Windows.Controls (the library is the same PresentationFramework.dll).
The namespace of the Class TextBlock is the same :System.Windows.Controls (the library is also the same PresentationFramework.dll).
It's usual (The Visual C# Express do it) to declare more namespaces than we need.
The method InitializeComponent, inside the constructor method overrided, loads all the content included in the Window.
The override of the event Loaded (our name is: OnLoaded) set a value to the property Text of the object Text1.
Interesting to note that the definition of the values for the position of the text is by the expressions: Canvas.Top="100" Canvas.Left="100". At the .XAML file we can use sometimes the name of a Class like an object.
Now, look 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>
We have here the same concepts. AppStartingUp is our name for the override of the event Startup that occurs when the executable starts - we can think like it was the Main of the usual programs.
Look 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){
Window1 mainWindow = new Window1();
mainWindow.Show();
}
}
}
We are creating here an object of the subclass Window1. And using its method: Show().
The namespace of Application is System.Windows and the library is PresentationFramework.dll.
OK. All saved at the folder Ba1, we can say that these four file are our "program". At the next lesson we will see how to "compile" it.
PREVIOUS LESSON NEXT LESSON T.CONTENTS HOMEPAGE
