Building Examples That Use a Demo Method and a TextBlock Control
Microsoft Silverlight will reach end of support after October 2021. Learn more.
This topic explains how to build and run Silverlight reference and conceptual examples that have the following characteristics:
They demonstrate types and members that are not part of the user interface of a Silverlight-based application, such as the Dictionary<TKey, TValue> and Thread classes.
They have a static Demo method that runs the example code.
They write their output to a TextBlock control.
Note: |
---|
Many Silverlight examples are similar to their corresponding .NET Framework examples, except that they have a Demo method instead of a Main method and they write to a TextBlock control instead of using Console.WriteLine. |
Building the Silverlight examples consists of the following steps:
Create a Silverlight-based application by using a project template.
Modify the MainPage.xaml file.
Modify the MainPage.xaml.cs or MainPage.xaml.vb file.
Add a new file that contains the example code.
Build and run the application.
Procedures
To create a Silverlight project in Visual Studio 2008
Start Visual Studio 2008.
On the File menu, point to New, and then click Project.
In the Project Types pane of the New Project dialog box, click Visual C# or Visual Basic.
In the Templates list, click Silverlight Application.
Note: If Silverlight Application does not appear in the Templates list, install Silverlight Tools for Visual Studio 2010 from the Microsoft Download Center.
Type the application name, enter its location, and then click OK.
In the Add Silverlight Application dialog box, click Dynamically generate an HTML test page to host Silverlight within this project, and then click OK.
Note: Optionally, you can add a new Web site or link to an existing Web site, but this is not necessary for running these examples. They can be run locally in the browser.
To modify the MainPage.xaml file
Open the default MainPage.xaml file for your Silverlight project.
Change the width of the default UserControl to 1024 or larger, and change its height to 768 or larger, to accommodate the output from the typical example.
Insert the following TextBlock control into the default Grid control.
<TextBlock x:Name = "outputBlock" FontSize="12" TextWrapping="Wrap" />
The following code shows the modified MainPage.xaml file.
<UserControl x:Class="Project1.MainPage" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:d=https://schemas.microsoft.com/expression/blend/2008 xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"> <Grid x:Name="LayoutRoot" Background="White"> <TextBlock x:Name="outputBlock" FontSize="12" TextWrapping="Wrap" /> </Grid> </UserControl>
To modify the code file for page.xaml
Open the MainPage.xaml.cs or MainPage.xaml.vb file for your application.
After the InitializeComponent statement in the MainPage() constructor, add a call to the static (Shared in Visual Basic) Example.Demo method. Pass the TextBlock control to the method.
The following code shows the modified MainPage.xaml.cs file:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SilverlightApplication { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); Example.Demo(outputBlock); } } }
The following code shows the modified MainPage.xaml.vb file:
Partial Public Class MainPage Inherits UserControl Public Sub New() InitializeComponent() Example.Demo(outputBlock) End Sub End Class
To add a new file that contains example code to your Silverlight project
Copy the example code from a Silverlight topic.
In Solution Explorer, right-click on the project's name, point to Add, and then click New Item to add a new file to the project.
In the Add New Item dialog box, click Code File from the Templates list, name the file Example.cs or Example.vb, and then click the Add button.
Open the Example.cs or Example.vb file.
Paste the example code into the new code file.
The following code demonstrates Example.cs:
class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { outputBlock.Text += "This message is never seen by users.\n"; } }
The following code demonstrates Example.vb:
Class Example Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) outputBlock.Text &= "This message is never seen by users." & vbCrLf End Sub End Class
You are now ready to build and run the example.