构建具有静态 Windows Phone 8 TextBlock 控件的示例

2014/6/18

适用于: Windows Phone 8 和 Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

 

本主题说明如何构建并运行具有以下特点的 Windows Phone 示例:

  • 它们演示不属于 Windows Phone 应用用户界面的类型和成员,如 StringRegexDictionary<TKey, TValue> 类。

  • 它们具有运行示例代码的静态 Demo 方法。

  • 它们将输出写入 TextBlock 控件。

备注

许多 Windows Phone 示例类似于它们对应的 .NET Framework 示例,只是它们具有 Demo 方法而不是 Main 方法,并且它们写入 TextBlock 控件而不是使用 Console.WriteLine。

构建 Windows Phone 示例包含以下步骤,这些步骤在“步骤”一节中进行了详细介绍:

  1. 使用 Windows Phone 应用 项目模板创建新项目。

  2. 修改 MainPage.xaml 文件。

  3. 修改 MainPage.xaml.cs 或 MainPage.xaml.vb 文件。

  4. 添加包含示例代码的新文件。

  5. 构建并运行此应用。

步骤

在 Visual Studio 中创建 Windows Phone 应用项目的步骤

  1. 启动 Visual Studio。

  2. “文件”菜单上指向“新建”,再单击“项目”

  3. “新建项目”对话框的“项目类型”窗格中,单击“Visual C#”“Visual Basic”

  4. “模板”列表中,单击 “Windows Phone 应用”

    备注

    如果列表中未显示手机模板,请参见获取 SDK,以了解有关安装 Windows Phone 软件开发工具包的信息。

  5. 键入应用名称,输入其位置,然后单击“确定”

  6. 选择应用将面向的 Windows Phone 平台。

    重要说明重要说明:

    某些示例调用在 Windows Phone OS 7.0 或 Windows Phone OS 7.1 中不可用的类型或成员,并且可能无法在这些平台上成功编译或运行。

修改 MainPage.xaml 文件的步骤

  1. 打开应用项目的默认 MainPage.xaml 文件。

  2. 将以下 TextBlock 控件插入到默认 Grid 控件。

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

    下面的代码显示已修改的 MainPage.xaml 文件的结果部分。

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

修改 MainPage.xaml 的代码文件的步骤

  1. 打开应用的 MainPage.xaml.cs 或 MainPage.xaml.vb 文件。

  2. 在主 Page() 构造函数的 InitializeComponent 语句后面添加对 static(在 Visual Basic 中为 Shared)Example.Demo 方法的调用。将名为 outputBlock 的 TextBlock 控件传递给该方法。

  3. 下面的代码显示已修改的 MainPage.xaml.cs 或 MainPage.xaml.vb 代码文件:

    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);
            }
        }
    }
    

将包含示例代码的新文件添加到 Windows Phone 项目的步骤

  1. 复制 Windows Phone 主题中的示例代码。

  2. “解决方案资源管理器”中,右键单击该项目的名称,指向“添加”,然后单击“新建项”将新文件添加到该项目。

  3. “添加新项”对话框中,单击“模板”列表中的“代码文件”,将该文件命名为 Example.cs 或 Example.vb,然后单击“添加”按钮。

  4. 打开 Example.cs 或 Example.vb 文件。

  5. 将示例代码粘贴到新代码文件中。

  6. 下面的代码演示 Example.cs 或 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;
    
       }
    }
    

    您现在就可以构建并运行该示例了。

请参阅

其他资源

Windows Phone 8 的概念和体系结构