40 likes | 202 Vues
This tutorial introduces Windows Presentation Foundation (WPF) and XAML by demonstrating how to create a simple button application. You'll learn how XAML elements correspond to WPF classes, with attributes directly mapping to properties and events in the class. An example is provided on creating a button with a red background and a click event that displays a message box. The tutorial also covers the basics of XAML code structure, including namespaces and the association with code-behind classes, ensuring a solid foundation for WPF development.
E N D
XAML & WPF • XAML element corresponds to a WPF class • Element's attributes have corresponding property/event in the class. • Example, <Button Background="Red"> No </Button> • Or use C#: Button btn = new Button(); btn.Background= Brushes.Red; btn.Content= "No";
WPF example – part 1 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation " xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SDKSample.AWindow" Title="Window with Button" Width="250" Height="100"> <!-- Add button to window --> <Button Name="button" Click="button_Click">Click Me! </Button> </Window> • x:Class associates • XAML def'n of window (above) • code-behind class (Awindow) on next slide • This example (pars 1 & 2) is from: http://msdn.microsoft.com/en-us/library/aa970268(v=vs.110).aspx
WPF example –part 2 using System.Windows; // Window, RoutedEventArgs, MessageBox namespace SDKSample { public partial class AWindow: Window { public AWindow() { // InitializeComponent call is required to merge the UI // that is defined in markup with this class, including // setting properties and registering event handlers InitializeComponent(); } void button_Click(object sender, RoutedEventArgs e) { // Show message box when button is clicked MessageBox.Show("Hello, from WPF app!"); } } }