WPF (Windows Presentation Foundation)
“`xml
“`
“`csharp
// MyWindow.xaml.cs
using System.Windows;
namespace MyNamespace
{
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
}
}
}
“`
This WPF example demonstrates a basic window with a centered text block displaying “Hello, WPF!”.
## Breakdown:
* **``:** Represents the main application window.
* `x:Class=”MyNamespace.MyWindow”`: Connects the XAML markup to the C# code-behind file `MyWindow.xaml.cs`.
* `xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”`: Default XML namespace for WPF core elements.
* `xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”`: XML namespace for XAML language features (like `x:Class`, `x:Name`).
* `Title`, `Height`, `Width`: Common window properties.
* **``:** A versatile layout panel that arranges its children in rows and columns. It’s often the root element inside a `Window`.
* **``:** A control used to display non-editable text.
* `Text=”Hello, WPF!”`: The actual text content.
* `HorizontalAlignment=”Center”`, `VerticalAlignment=”Center”`: Centers the TextBlock within its parent container (the Grid).
* `FontSize`, `FontWeight`: Text formatting properties.
In the C# code-behind:
* `public partial class MyWindow : Window`: Declares a partial class `MyWindow` that inherits from `System.Windows.Window`. The `partial` keyword allows the class definition to be split across multiple files (one for XAML-generated code, one for your custom logic).
* `InitializeComponent()`: This method is automatically generated by Visual Studio during compilation (from the XAML) and is crucial for initializing the UI elements defined in the XAML and connecting them to the code-behind. It’s *always* called in the constructor.