Building examples that have static TextBlock controls for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

 

This topic explains how to build and run Windows Phone examples that have the following characteristics:

  • They demonstrate types and members that are not part of the user interface of a Windows Phone app, such as the String, Regex, and Dictionary<TKey, TValue> classes.

  • They have a static Demo method that runs the example code.

  • They write their output to a TextBlock control.

Note

Many Windows Phone 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 Windows Phone examples consists of the following steps, which are covered in detail in the Procedures section:

  1. Use the Windows Phone App project template to create a new project.

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

Procedures

To create a Windows Phone App project in Visual Studio

  1. Start Visual Studio.

  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 Windows Phone App.

    Note

    If phone templates do not appear in the list, see Get the SDK for information about installing the Windows Phone Software Development Kit.

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

  6. Select the Windows Phone platform that your app will target.

    Important noteImportant Note:

    Some of the examples call types or members that are not available in Windows Phone OS 7.0 or Windows Phone OS 7.1 and may not compile or run successfully on those platforms.

To modify the MainPage.xaml file

  1. Open the default MainPage.xaml file of the app project.

  2. Insert the following TextBlock control into the default Grid control.

    <TextBlock x:Name="outputBlock" FontSize="12" TextWrapping="Wrap">
         </TextBlock>
    

    The following code shows the resulting portion of the modified MainPage.xaml file.

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
       <TextBlock x:Name="outputBlock" FontSize="12" TextWrapping="Wrap">
       </TextBlock>
    </Grid>
    

To modify the code file for MainPage.xaml

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

  2. After the InitializeComponent statement in the MainPage() constructor, add a call to the static (Shared in Visual Basic) Example.Demo method. Pass a TextBlock control named outputBlock to the method.

  3. The following code shows the modified MainPage.xaml.cs or MainPage.xaml.vb code file:

    Partial Public Class Page
       Inherits PhoneApplicationPage
    
       Public Sub New()
          InitializeComponent()
    
          Example.Demo(outputBlock)
       End Sub
    End Class
    
    using Microsoft.Phone.Controls;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    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 ApolloApplication
    {
        public partial class Page : PhoneApplicationPage
        {
            public Page()
            {
                InitializeComponent();
    
                Example.Demo(outputBlock);
            }
        }
    }
    

To add a new file that contains example code to your Windows Phone project

  1. Copy the example code from a Windows Phone 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 or Example.vb:

    Class Example
       Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
    
          outputBlock.Text += "This is sample output." + Environment.NewLine
    
       End Sub 
    End Class 
    
    using System;
    using Microsoft.Phone.Controls;
    
    class Example
    {
       public static void Demo(System.Windows.Controls.TextBlock outputBlock)
       {
    
          outputBlock.Text += "This is sample output." + Environment.NewLine;
    
       }
    }
    

    You are now ready to build and run the example.

See Also

Other Resources

Concepts and architecture for Windows Phone 8