Freigeben über


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.

NoteNote:

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:

  1. Create a Silverlight-based application by using a project template.

  2. Modify the MainPage.xaml file.

  3. Modify the MainPage.xaml.cs or MainPage.xaml.vb file.

  4. Add a new file that contains the example code.

  5. Build and run the application.

Procedures

To create a Silverlight project in Visual Studio 2008

  1. Start Visual Studio 2008.

  2. On the File menu, point to New, and then click Project.

  3. In the Project Types pane of the New Project dialog box, click Visual C# or Visual Basic.

  4. In the Templates list, click Silverlight Application.

    NoteNote:

    If Silverlight Application does not appear in the Templates list, install Silverlight Tools for Visual Studio 2010 from the Microsoft Download Center.

  5. Type the application name, enter its location, and then click OK.

  6. In the Add Silverlight Application dialog box, click Dynamically generate an HTML test page to host Silverlight within this project, and then click OK.

    NoteNote:

    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

  1. Open the default MainPage.xaml file for your Silverlight project.

  2. 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.

  3. 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

  1. Open the MainPage.xaml.cs or MainPage.xaml.vb file for your application.

  2. 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.

  3. 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

  1. Copy the example code from a Silverlight topic.

  2. 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.

  3. 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.

  4. Open the Example.cs or Example.vb file.

  5. Paste the example code into the new code file.

  6. 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.

See Also

Concepts