Appendix H. XAML Interface in Code


Anything in XAML can be done programmatically, due to the fact that XAML elements represent classes in WPF. Consider the following XAML declaration of a simple application:

     <Page         xmlns="http://schemas.microsoft.com/winfx/avalon/2005"         xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" >         <StackPanel>         <StackPanel             Orientation="Vertical"             Width="100"             HorizontalAlignment="Left" >                <TextBlock>First Block of Text</TextBlock>                <Button                 Content="Button 1" />            </StackPanel>         <StackPanel             Orientation="Vertical"             Width="100"             HorizontalAlignment="Left" >                <TextBlock>Second Block of Text</TextBlock>                <Button                 Content="Button 2" />            </StackPanel>         </StackPanel>     </Page> 

The following C# code declares the same controls and elements described in the preceding XAML declaration. Note that the C# code requires many more lines than the XAML representation, and the XAML representation is much clearer in terms of the hierarchy of elements. These are two of the advantages of using XAML over procedural code to declare a user interface.

     using System;     using System.Collections;     using System.Text;     using System.Windows;     using System.Windows.Controls;     using System.Windows.Documents;     using System.Windows.Navigation;     using System.Windows.Media;     using System.Windows.Media.Imaging;     using System.IO;     using System.Threading;     namespace SimpleApplication     {       public class MyApplication : Application       {         TextBlock txtElement1;         TextBlock txtElement2;         StackPanel rootPanel;         Button btnElement1;         StackPanel panel1;         StackPanel panel2;         Button btnElement2;         Window win;         protected override void OnStartup(StartupEventArgs e)         {            win = new System.Windows.Window(  );            rootPanel = new StackPanel(  );            panel1 = new StackPanel(  );            panel1.Orientation= System.Windows.Controls.Orientation.Vertical;            panel1.HorizontalAlignment=System.Windows.HorizontalAlignment.Left;            panel1.Width=100;            txtElement1 = new TextBlock(  );            txtElement1.Text = "First Block of Text";            btnElement1 = new Button(  );            btnElement1.Content = "Button 1";            panel1.Children.Add(txtElement1);            panel1.Children.Add(btnElement1);            panel2 = new StackPanel(  );            panel2.Orientation= System.Windows.Controls.Orientation.Vertical;            panel2.HorizontalAlignment=System.Windows.HorizontalAlignment.Left;            panel2.Width=100;            txtElement2 = new TextBlock(  );            txtElement2.Text = "Second Block of Text";            btnElement2 = new Button(  );            btnElement2.Content = "Button 2";            panel2.Children.Add(txtElement2);            panel2.Children.Add(btnElement2);            win.Content = rootPanel;            rootPanel.Children.Add(panel1);            rootPanel.Children.Add(panel2);            win.Show(  );         }       }       internal sealed class Test       {         [System.STAThread(  )]         public static void Main(  )         {           MyApplication app = new MyApplication(  );           app.Run(  );         }       }     } 




XAML in a Nutshell
XAML in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596526733
EAN: 2147483647
Year: 2007
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net