Translate

Sunday, 30 June 2013

What is XAML

XAML

XAML is an XML-based language for creating and initializing .NET objects. It’s used
in WPF as a human-authorable way of describing the UI, although you can use it for
a much larger range of CLR types than just those in WPF. Example 1-8 shows how
we declare the UI of our Window-derived class using XAML.

Example 1-8. Declaring a Window in XAML
<!-- Window1.xaml -->
<Window
x:Class="MyFirstWpfApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Hello, WPF">
<Button
x:Name="button"
Width="200"
Height="25"
Click="button_Click">Click me, baby, one more time!</Button>
</Window>

The root element, Window, is used to declare a portion of a class, the name of which is
contained in the Class attribute from the XAML XML namespace (declared with a
prefix of “x” using the “xmlns” XML namespace syntax). The two XML namespace
declarations pull in two commonly used namespaces for XAML work, the one for
XAML itself (the one with the “x” prefix) and the one for WPF (which we’ve
declared as the default for this XML file). You can think of the XAML


Example . C# equivalent of XAML from Example 1-8 (continued)
void InitializeComponent( ) {
// Initialize Window1
this.Title = "Hello, WPF";
// Initialize button
button = new Button( );
button.Width = 200;
button.Height = 25;
button.Click += button_Click;
this.AddChild(button);
     }
   }
 }

XAML was built to be as direct a mapping from XML to .NET as possible. Generally,
a XAML element is a .NET class name and a XAML attribute is the name of a
property or an event on that class. This makes XAML useful for more than just WPF
classes; pretty much any old .NET class that exposes a default constructor can be initialized
in a XAML file.
Notice that we don’t have the definition of the click event handler in this generated
class. For event handlers and other initializations and helpers, a XAML file is meant
to be matched with a corresponding code-behind file, which is a .NET language code
file that implements behavior in code “behind” the look defined in the XAML. Traditionally,
this file is named with a .xaml.cs extension and contains only the things not
defined in the XAML. With the XAML from Example 1-8 in place, we can reduce
our single-buttoned main window code-behind file to the code in Example 1-10.

Example . C# code-behind file
// Window1.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace MyFirstWpfApp {
public partial class Window1 : Window {
public Window1( ) {
InitializeComponent( );
}
void button_Click(object sender, RoutedEventArgs e) {
MessageBox.Show(...);
  }
 }
}

Notice the partial keyword modifying the Window1 class, which signals to the compiler
that the XAML-generated class is to be paired with this human-generated class to form
one complete class, each depending on the other. The partial Window1 class defined in
XAML depends on the code-behind partial class to call the InitializeComponent
method and to handle the click event. The code-behind class depends on the partial
Window1 class defined in XAML to implement InitializeComponent, thereby providing
the look of the main window (and related child controls).
Further, as mentioned, XAML is not just for visuals. For example, nothing is stopping
us from moving most of the definition of our custom MyApp class into a XAML
file (Example 1-11).

This reduces the MyApp code-behind file to the event handler in Example 1-12.
You may have noticed that we no longer have a Main entry point to create the
instance of the application-derived class and call its Run method. That’s because WPF
has a special project setting to specify the XAML file that defines the application
class, which appears in the msbuild project file.

Example . Declaring an application in XAML
<!-- MyApp.xaml -->
<Application
x:Class="MyFirstWpfApp.MyApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="AppStartup">
</Application>
Example 1-12. Application code-behind file
// MyApp.xaml.cs
using System;
using System.Windows;
namespace MyFirstWpfApp {
public partial class MyApp : Application {
void AppStartup(object sender, StartupEventArgs e) {
Window window = new Window1( );
window.Show( );
  }
 }
}

No comments: