Here we display text using SilverLight TextBlock control programetically. So we follow some steps.

Step 1 create Silverlight project that has two files App.xaml and MainPage.xaml. This project also contains there code behind files App.xaml.cs and MainPage.xaml.cs respectively.

Here App.xaml file is loader for our Silverlight application and its code behind file App.xaml.cs uses to handle global level events of application for application start, exit and exceptions.

MainPage.xaml is actually strat page when application runs. It contains Silverlight control means it has XAML declarative format for UI. MainPage.xaml.cs uses to handle code behind work of Silverlight controls. MainPage.xaml is like:

<UserControl x:Class="FirstExample.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

<Grid x:Name="LayoutRoot">

</Grid>

</UserControl>

Here our Silverlight Grid Control is in UserControl. That usercontrol has all silverlight control that are related to our silverlight UI page. x:Name represents unique name of silverlight contro <Grid> that is identify on Main.xaml.cs code behind page for fruther coding.

Step 2 In Main.xaml.cs file we create some code for text display. First of all we write code in class constructor after InitializeComponent() method. InitializeComponent() method is responsible to load XAML using LoadComponent() method into memory.

public MainPage()

{

InitializeComponent();

TextBlock txtword = new TextBlock

{

Text = "Hello world in Silver Light",//for text that will be display

Foreground = new SolidColorBrush(Colors.Green),//color of text

FontSize = 28,//size of text

FontFamily = new FontFamily("Arial"),//font type for text

};

LayoutRoot.Children.Add(txtword);//add TextBlock to Grid as child control and LayoutRoot is Grid Name

}

Explain:

TextBlock is a class in System.Windows.Contros namespaces to display small text amout. Text is property of TextBlock class is use to display text and Font is also a property to uses increses and decreses font size of displaying text. Foreground is another property of TextBlock class . For that value we create a new SolidColorBrush and describe its Color values. FontFamily property uses to assign a font family value we create new FontFamily .At last we add TextBlock to Our Grid as Children

OU