- Back to Home »
- Windows Phone apps »
- Windows Phone 8 apps development Part-3 XAML
Posted by :
Sudhir Chekuri
Tuesday, 1 October 2013
Introduction to XAML
XAML stands for Extensible application markup language.
It is a case sensitive language.
.xaml is the extension for xaml files.
It is used for declaring windows phone UI along with WPF and silverlight.
XAML elements are objects in the System.Windows.Controls namespace.
Every element contains properties that defines how it appears on the screen like width, height, color... These values of properties can also be changed using C# code which effects in UI.
xmlns stands for xml namespaces used to add namespaces in xaml.
x:Name property is used to specify name of an element using which you can control that element using C# code.
Declaring xaml object
xaml object is created using opening and closing tags.
opening tag contain object name surrounded by angular braces and closing tag contain object name with forward slash as suffix surrounded by angular braces.
<Canvas> </Canvas>
Some of the objects in xaml such as canvas, stack panel, grid,... are capable of holding other controls inside them.
<Canvas> <Rectangle> </Rectangle> </Canvas>
We can also use single tag in place of opening and closing tags for some elements.
<Canvas> <Rectangle /> </Canvas>
We can also declare attributes or properties for the elements within the tags
<Canvas> <Rectangle Name="R1" Width="120" Height="120" Fill="Blue" /> </Canvas>
Everythind you do in XAML can be done with code.
The above object can be created using C# code as follows
Rectangle R1 = new Rectangle( );
R1.Width=120;
R1.Height=120;
R1.Fill = new SolidColorBrush(Colors.Blue);
XAML follows hierarchy in describing elements. It is extensible which allows developers to create their own reusable controls.
Lot of standard controls in the toolbox can be used to design the app without writing XAML.
Toolkit controls are also available which can be used by installing sdk and adding namespace.
Signature controls like panorama, pivot, sticky heads, jumplist, gridview.
It is a case sensitive language.
.xaml is the extension for xaml files.
It is used for declaring windows phone UI along with WPF and silverlight.
XAML elements are objects in the System.Windows.Controls namespace.
Every element contains properties that defines how it appears on the screen like width, height, color... These values of properties can also be changed using C# code which effects in UI.
xmlns stands for xml namespaces used to add namespaces in xaml.
x:Name property is used to specify name of an element using which you can control that element using C# code.
Declaring xaml object
xaml object is created using opening and closing tags.
opening tag contain object name surrounded by angular braces and closing tag contain object name with forward slash as suffix surrounded by angular braces.
<Canvas> </Canvas>
Some of the objects in xaml such as canvas, stack panel, grid,... are capable of holding other controls inside them.
<Canvas> <Rectangle> </Rectangle> </Canvas>
We can also use single tag in place of opening and closing tags for some elements.
<Canvas> <Rectangle /> </Canvas>
We can also declare attributes or properties for the elements within the tags
<Canvas> <Rectangle Name="R1" Width="120" Height="120" Fill="Blue" /> </Canvas>
Everythind you do in XAML can be done with code.
The above object can be created using C# code as follows
Rectangle R1 = new Rectangle( );
R1.Width=120;
R1.Height=120;
R1.Fill = new SolidColorBrush(Colors.Blue);
XAML follows hierarchy in describing elements. It is extensible which allows developers to create their own reusable controls.
Lot of standard controls in the toolbox can be used to design the app without writing XAML.
Toolkit controls are also available which can be used by installing sdk and adding namespace.
Signature controls like panorama, pivot, sticky heads, jumplist, gridview.
Windows phone 8 basic page XAML code
<phone:PhoneApplicationPage
x:Class="Sampleapp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
</Grid>
</Grid>
</phone:PhoneApplicationPage>
Explanation
It contain a grid named as LayoutRoot with Transparent as background color.
It contains two rows with height as auto and *.
auto means that row height is based on the content inside it.
* means the remaining space is the height of the second row.
It contain a stackpanel named as TitlePanel which is present in the row 0 of grid with specific margin.
Stack panel is a control used to hold other controls inside it to get a good design in xaml.
Stack panel holds two textblocks and the other grid row contains nothing.
There is a PhoneApplicationPage class with some sub classes.
Frame contains a page and page contains content.
Each page is identified by URI and every page is stateless.
Prerequisites for developing phone 8 apps
x:Class="Sampleapp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
</Grid>
</Grid>
</phone:PhoneApplicationPage>
Explanation
It contain a grid named as LayoutRoot with Transparent as background color.
It contains two rows with height as auto and *.
auto means that row height is based on the content inside it.
* means the remaining space is the height of the second row.
It contain a stackpanel named as TitlePanel which is present in the row 0 of grid with specific margin.
Stack panel is a control used to hold other controls inside it to get a good design in xaml.
Stack panel holds two textblocks and the other grid row contains nothing.
There is a PhoneApplicationPage class with some sub classes.
Frame contains a page and page contains content.
Each page is identified by URI and every page is stateless.
Prerequisites for developing phone 8 apps
Install Visual Studio 2012 phone express with SDK 8.0 for developing windows phone 8 apps.
Open visual studio
Steps Creating first phone 8 app
file – new – project – select C# Windows Phone
Select Template Windows Phone app
Open visual studio
Steps Creating first phone 8 app
file – new – project – select C# Windows Phone
Select Template Windows Phone app
Name of your project will be PhoneApp1 by default
When you click on Ok you will see a dialog asking for selection of Target Windows Phone OS. You can select 8.0 and click on Ok for windows phone 8 app.
Screens here are called pages.
MainPage.xaml is the first default page in which we will work and you can add more page to navigate from one page to other.
Basic XAML code in MainPage.xaml page
Screens here are called pages.
MainPage.xaml is the first default page in which we will work and you can add more page to navigate from one page to other.
Basic XAML code in MainPage.xaml page
<phone:PhoneApplicationPage
x:Class="PhoneApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResourcePhoneFontFamilyNormal}"
FontSize="{StaticResourcePhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot"Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel"Grid.Row="1" Margin="12,0,12,0">
</Grid>
</Grid>
</phone:PhoneApplicationPage>
<Grid x:Name="ContentPanel"Grid.Row="1" Margin="12,0,12,0">
<Button>Click ME</Button>
</Grid>
This button will occupy the entire ContentPanel.
Adding XAML button with specific size inside Grid
This button will occupy the entire ContentPanel.
Adding XAML button with specific size inside Grid
<Grid x:Name="ContentPanel"Grid.Row="1" Margin="12,0,12,0">
<Button Height="200" Width="200">Click ME</Button>
</Grid>
Now this square shaped button will be aligned at the center of the grid.
Now this square shaped button will be aligned at the center of the grid.
To align it at the top left corner of the grid use the below code
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Height="200" Width="200"
VerticalAlignment="Top"
HorizontalAlignment="Left"
>Click ME</Button>
</Grid>
Now I will add background color to the button using below xaml code
<Grid x:Name="ContentPanel"Grid.Row="1" Margin="12,0,12,0">
<Button Height="200" Width="200"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Background="Red"
>Click ME</Button>
</Grid>