面向多个平台的库的应用程序资源App Resources for Libraries That Target Multiple Platforms

可以使用.NET Framework可移植类库项目类型以确保可以从多个平台访问类库中的资源。You can use the .NET Framework Portable Class Library project type to ensure that resources in your class libraries can be accessed from multiple platforms. 该项目类型可以在 Visual Studio 2012 中,面向.NET Framework 类库的可移植子集。This project type is available in Visual Studio 2012 and targets the portable subset of the .NET Framework class library. 使用可移植类库可确保你的库可以从桌面应用、 Silverlight 应用程序、 Windows Phone 应用进行访问和Windows 8.x 应用商店Windows 8.x Store应用。Using a Portable Class Library ensures that your library can be accessed from desktop apps, Silverlight apps, Windows Phone apps, and Windows 8.x 应用商店Windows 8.x Store apps.

重要

因为可移植类库项目面向的.NET 实现仅非常特定的专项,我们强烈建议不要在新的应用程序开发中使用它们。Because Portable Class Library projects target only a very specific subset of .NET implementations, we strongly discourage their use in new application development. 推荐的替代是面向所有支持特定版本的.NET Standard 的.NET 实现的.NET Standard 库。The recommended replacement is a .NET Standard library, which targets all .NET implementations that support a specific version of the .NET Standard. 有关详细信息,请参阅 .NET StandardFor more information, see .NET Standard.

可移植类库项目使非常有限的一个子集中的类型System.Resources命名空间可用于您的应用程序,但它允许你使用ResourceManager类检索资源。The Portable Class Library project makes only a very limited subset of the types in the System.Resources namespace available to your application, but it does allow you to use the ResourceManager class to retrieve resources. 但是,如果你使用 Visual Studio 创建应用,则应使用 Visual Studio 创建的强类型包装器而不是直接使用 ResourceManager 类。However, if you are creating an app by using Visual Studio, you should use the strongly typed wrapper created by Visual Studio instead of using the ResourceManager class directly.

若要在 Visual Studio 中创建的强类型化的包装器,设置主资源文件访问修饰符到 Visual Studio 资源设计器公共To create a strongly typed wrapper in Visual Studio, set the main resource file's Access Modifier in the Visual Studio Resource Designer to Public. 这将创建一个包含强类型 ResourceManager 包装器的 [resourceFileName].designer.cs 或 [resourceFileName].designer.vb 文件。This creates a [resourceFileName].designer.cs or [resourceFileName].designer.vb file that contains the strongly typed ResourceManager wrapper. 有关使用强类型的资源包装器的详细信息,请参阅中的"生成强类型资源类"一节Resgen.exe (资源文件生成器)主题。For more information about using a strongly typed resource wrapper, see the "Generating a Strongly Typed Resource Class" section in the Resgen.exe (Resource File Generator) topic.

可移植类库中的资源管理器Resource Manager in the Portable Class Library

在可移植类库项目中,对资源的所有访问都由ResourceManager类。In a Portable Class Library project, all access to resources is handled by the ResourceManager class. 因为中的类型System.Resources命名空间,如ResourceReaderResourceSet,是从可移植类库项目不可访问,它们不能用于访问资源。Because types in the System.Resources namespace, such as ResourceReader and ResourceSet, are not accessible from a Portable Class Library project, they cannot be used to access resources.

可移植类库项目包括四个ResourceManager下表中列出的成员。The Portable Class Library project includes the four ResourceManager members listed in the following table. 这些构造函数和方法使你能够实例化 ResourceManager 对象和检索字符串资源。These constructors and methods enable you to instantiate a ResourceManager object and retrieve string resources.

ResourceManager 成员ResourceManager member 描述Description
ResourceManager(String, Assembly) 创建一个可访问在指定程序集中找到的已命名资源文件的 ResourceManager 实例。Creates a ResourceManager instance to access the named resource file found in the specified assembly.
ResourceManager(Type) 创建一个与指定类型对应的 ResourceManager 实例。Creates a ResourceManager instance that corresponds to the specified type.
GetString(String) 检索当前区域性的已命名资源。Retrieves a named resource for the current culture.
GetString(String, CultureInfo) 检索属于指定区域性的已命名资源。Retrieves a named resource belonging to the specified culture.

排除其他ResourceManager成员从序列化对象、 非字符串数据和映像的可移植类库方法不能检索从资源文件。The exclusion of other ResourceManager members from the Portable Class Library means that serialized objects, non-string data, and images cannot be retrieved from a resource file. 若要从可移植类库中使用的资源,应以字符串形式存储的所有对象数据。To use resources from a Portable Class Library, you should store all object data in string form. 例如,你可以通过将数值转换为字符串来将其存储在资源文件中,并且你可以检索它们,然后使用数字数据类型的 ParseTryParse 方法将其转换回数字。For example, you can store numeric values in a resource file by converting them to strings, and you can retrieve them and then convert them back to numbers by using the numeric data type's Parse or TryParse method. 你可通过调用 Convert.ToBase64String 方法将图像或其他二进制数据转换为字符串表示形式,并通过调用 Convert.FromBase64String 方法将其还原到字节数组。You can convert images or other binary data to a string representation by calling the Convert.ToBase64String method, and restore them to a byte array by calling the Convert.FromBase64String method.

可移植类库和 Windows 应用商店应用The Portable Class Library and Windows Store Apps

可移植类库项目将资源存储在.resx 文件中,然后编译为.resources 文件并在编译时嵌入在主程序集或附属程序集。Portable Class Library projects store resources in .resx files, which are then compiled into .resources files and embedded in the main assembly or in satellite assemblies at compile time. 另一方面,Windows 8.x 应用商店Windows 8.x Store应用需在 .resw 文件中存储资源,这些文件随后将被编译为单个包资源索引 (PRI) 文件。Windows 8.x 应用商店Windows 8.x Store apps, on the other hand, require resources to be stored in .resw files, which are then compiled into a single package resource index (PRI) file. 但是,尽管不兼容的文件格式,可移植类库将工作Windows 8.x 应用商店Windows 8.x Store应用。However, despite the incompatible file formats, your Portable Class Library will work in a Windows 8.x 应用商店Windows 8.x Store app.

若要从 Windows 8.x 应用商店Windows 8.x Store应用使用你的类库,请在 Windows 应用商店应用项目中添加对类库的引用。To consume your class library from a Windows 8.x 应用商店Windows 8.x Store app, add a reference to it in your Windows Store app project. Visual Studio 以透明方式将资源提取到.resw 文件程序集,并使用它来生成 Windows 运行时可以从其提取资源的 PRI 文件。Visual Studio will transparently extract the resources from your assembly into a .resw file and use it to generate a PRI file from which the Windows Runtime can extract resources. 在运行时,Windows 运行时执行的代码在可移植类库,但它从 PRI 文件中检索可移植类库的资源。At run time, the Windows Runtime executes the code in your Portable Class Library, but it retrieves your Portable Class Library's resources from the PRI file.

如果你的可移植类库项目包含本地化的资源,使用中心辐射模型以将其部署只需根据需要对桌面应用程序中的库。If your Portable Class Library project includes localized resources, you use the hub-and-spoke model to deploy them just as you would for a library in a desktop app. 若要在 Windows 8.x 应用商店Windows 8.x Store应用中使用主资源文件和所有本地化的资源文件,可添加对主程序集的引用。To consume your main resource file and any localized resource files in your Windows 8.x 应用商店Windows 8.x Store app, you add a reference to the main assembly. 在编译时,Visual Studio 会将你的主资源文件以及所有本地化资源文件中的资源提取到单独的 .resw 文件中。At compile time, Visual Studio extracts the resources from your main resource file and any localized resource files into separate .resw files. 然后将.resw 文件编译为单个 PRI 文件中的 Windows 运行时在运行时访问。It then compiles the .resw files into a single PRI file that the Windows Runtime accesses at run time.

示例:非本地化可移植类库Example: Non-Localized Portable Class Library

下面的简单、 非本地化可移植类库示例使用的资源来存储列的名称并确定要为表格数据保留的字符数。The following simple, non-localized Portable Class Library example uses resources to store the names of columns and to determine the number of characters to reserve for tabular data. 此示例使用名为 LibResources.resx 的文件存储下表中列出的字符串资源。The example uses a file named LibResources.resx to store the string resources listed in the following table.

资源名称Resource name 资源值Resource value
BornBorn BirthdateBirthdate
BornLengthBornLength 1212
HiredHired 雇佣日期Hire Date
HiredLengthHiredLength 1212
IdID IdID
ID.LengthID.Length 1212
名称Name 名称Name
NameLengthNameLength 2525
标题Title Employee DatabaseEmployee Database

下面的代码定义UILibrary类,该类使用名为资源管理器包装resources由 Visual Studio 生成时访问修饰符文件更改为公共.The following code defines a UILibrary class that uses the Resource Manager wrapper named resources generated by Visual Studio when the Access Modifier for the file is changed to Public. UILibrary 类根据需要分析字符串数据。The UILibrary class parses the string data as necessary. .. 请注意,该类位于 MyCompany.Employees 命名空间中。Note that the class is in the MyCompany.Employees namespace.

using System;
using System.Resources;
using MyCompany.Employees;

[assembly: NeutralResourcesLanguage("en-US")]

namespace MyCompany.Employees
{
   public class UILibrary
   {
      private const int nFields = 4;

      public static string GetTitle()
      {
         string retval = LibResources.Born; 
         if (String.IsNullOrEmpty(retval))
            retval = "";

         return retval;
      }

      public static string[] GetFieldNames()
      {
         string[] fieldnames = new string[nFields];
         fieldnames[0] = LibResources.Name;
         fieldnames[1] = LibResources.ID;
         fieldnames[2] = LibResources.Born;
         fieldnames[3] = LibResources.Hired;
         return fieldnames;
      }

      public static int[] GetFieldLengths()
      {
         int[] fieldLengths = new int[nFields];
         fieldLengths[0] = Int32.Parse(LibResources.NameLength);
         fieldLengths[1] = Int32.Parse(LibResources.IDLength);
         fieldLengths[2] = Int32.Parse(LibResources.BornLength);
         fieldLengths[3] = Int32.Parse(LibResources.HiredLength);
         return fieldLengths;
      }
   }
}
Imports System.Resources

<Assembly: NeutralResourcesLanguage("en-US")> 

Public Class UILibrary
   Private Const nFields As Integer = 4

   Public Shared Function GetTitle() As String
      Dim retval As String = My.Resources.LibResources.Title
      If String.IsNullOrEmpty(retval) Then retval = "<No value>"

      Return retval
   End Function

   Public Shared Function GetFieldNames() As String()
      Dim fieldnames(nFields - 1) As String
      fieldnames(0) = My.Resources.LibResources.Name
      fieldnames(1) = My.Resources.LibResources.ID
      fieldnames(2) = My.Resources.LibResources.Born
      fieldnames(3) = My.Resources.LibResources.Hired
      Return fieldnames
   End Function

   Public Shared Function GetFieldLengths() As Integer()
      Dim fieldLengths(nFields - 1) As Integer
      fieldLengths(0) = Int32.Parse(My.Resources.LibResources.NameLength)
      fieldLengths(1) = Int32.Parse(My.Resources.LibResources.IDLength)
      fieldLengths(2) = Int32.Parse(My.Resources.LibResources.BornLength)
      fieldLengths(3) = Int32.Parse(My.Resources.LibResources.HiredLength)
      Return fieldLengths
   End Function
End Class

以下代码阐释如何从控制台模式应用访问 UILibrary 类及其资源。The following code illustrates how the UILibrary class and its resources can be accessed from a console-mode app. 它需要对 UILibrary.dll 要添加到控制台应用程序项目的引用。It requires a reference to UILibrary.dll to be added to the console app project.

using System;
using System.Collections.Generic;
using MyCompany.Employees;
 
class Program
{
   static void Main()
   {
     // Get the data from some data source.
      var employees = InitializeData();

      // Display application title.
      string title = UILibrary.GetTitle();
      int start = (Console.WindowWidth + title.Length) / 2;
      string titlefmt = String.Format("{{0,{0}{1}", start, "}");
      Console.WriteLine(titlefmt, title);
      Console.WriteLine();

      // Retrieve resources.
      string[] fields = UILibrary.GetFieldNames();
      int[] lengths = UILibrary.GetFieldLengths();
      string fmtString = String.Empty;
      // Create format string for field headers and data.
      for (int ctr = 0; ctr < fields.Length; ctr++)
         fmtString += String.Format("{{{0},-{1}{2}{3}   ", ctr, lengths[ctr], ctr >= 2 ? ":d" : "", "}");

      // Display the headers.
      Console.WriteLine(fmtString, fields);
      Console.WriteLine();
      // Display the data.
      foreach (var e in employees)
         Console.WriteLine(fmtString, e.Item1, e.Item2, e.Item3, e.Item4);

      Console.ReadLine();
   }

   private static List<Tuple<String, String, DateTime, DateTime>> InitializeData() 
   {
      List<Tuple<String, String, DateTime, DateTime>> employees = new List<Tuple<String, String, DateTime, DateTime>>();
      var t1 = Tuple.Create("John", "16302", new DateTime(1954, 8, 18), new DateTime(2006, 9, 8));
      employees.Add(t1);
      t1 = Tuple.Create("Alice", "19745", new DateTime(1995, 5, 10), new DateTime(2012, 10, 17));
      employees.Add(t1);
      return employees;
   }
}
Imports MyCompany.Employees
Imports System.Collections.Generic

Module Module1

   Sub Main()
      ' Get the data from some data source.
      Dim employees = InitializeData()

      ' Display application title.
      Dim title As String = UILibrary.GetTitle()
      Dim start As Integer = (Console.WindowWidth + title.Length) \ 2
      Dim titlefmt As String = String.Format("{{0,{0}{1}", start, "}")
      Console.WriteLine(titlefmt, title)
      Console.WriteLine()

      ' Retrieve resources.
      Dim fields() As String = UILibrary.GetFieldNames()
      Dim lengths() As Integer = UILibrary.GetFieldLengths()
      Dim fmtString As String = String.Empty
      ' Create format string for field headers and data.
      For ctr = 0 To fields.Length - 1
         fmtString += String.Format("{{{0},-{1}{2}{3}   ", ctr, lengths(ctr), IIf(ctr >= 2, ":d", ""), "}")
      Next
      ' Display the headers.
      Console.WriteLine(fmtString, fields)
      Console.WriteLine()
      ' Display the data.
      For Each e In employees
         Console.WriteLine(fmtString, e.Item1, e.Item2, e.Item3, e.Item4)
      Next
      Console.ReadLine()
   End Sub

   Private Function InitializeData() As List(Of Tuple(Of String, String, Date, Date))
      Dim employees As New List(Of Tuple(Of String, String, Date, Date))
      Dim t1 = Tuple.Create("John", "16302", #8/18/1954#, #9/8/2006#)
      employees.Add(t1)
      t1 = Tuple.Create("Alice", "19745", #5/10/1995#, #10/17/2012#)
      employees.Add(t1)
      Return employees
   End Function
End Module

下列代码阐释了如何从 UILibrary应用访问 Windows 8.x 应用商店Windows 8.x Store 类及其资源。The following code illustrates how the UILibrary class and its resources can be accessed from a Windows 8.x 应用商店Windows 8.x Store app. 它需要对 UILibrary.dll 要添加到 Windows 应用商店应用项目的引用。It requires a reference to UILibrary.dll to be added to the Windows Store app project.

using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using MyCompany.Employees;

namespace ConsumerCS
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class BlankPage : Page
    {
        public BlankPage()
        {
            this.InitializeComponent();

         }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
           Example.DisplayData(outputBlock);
       }
    }
}

public class Example
{
    static public void DisplayData(Windows.UI.Xaml.Controls.TextBlock outputBlock)
    {
        // Get the data from some data source.
        var employees = InitializeData();
        outputBlock.FontFamily = new FontFamily("Courier New");      
        // Display application title.
        string title = UILibrary.GetTitle();
        outputBlock.Text += title + Environment.NewLine + Environment.NewLine;

        // Retrieve resources.
        string[] fields = UILibrary.GetFieldNames();
        int[] lengths = UILibrary.GetFieldLengths();
        string fmtString = String.Empty;
        // Create format string for field headers and data.
        for (int ctr = 0; ctr < fields.Length; ctr++)
            fmtString += String.Format("{{{0},-{1}{2}{3}   ", ctr, lengths[ctr], ctr >= 2 ? ":d" : "", "}");

        // Display the headers.
        outputBlock.Text += String.Format(fmtString, fields) + Environment.NewLine + Environment.NewLine;
        // Display the data.
        foreach (var e in employees)
            outputBlock.Text += String.Format(fmtString, e.Item1, e.Item2, e.Item3, e.Item4) + Environment.NewLine;
    }

    private static List<Tuple<String, String, DateTime, DateTime>> InitializeData()
    {
        List<Tuple<String, String, DateTime, DateTime>> employees = new List<Tuple<String, String, DateTime, DateTime>>();
        var t1 = Tuple.Create("John", "16302", new DateTime(1954, 8, 18), new DateTime(2006, 9, 8));
        employees.Add(t1);
        t1 = Tuple.Create("Alice", "19745", new DateTime(1995, 5, 10), new DateTime(2012, 10, 17));
        employees.Add(t1);
        return employees;
    }
}

示例:本地化的可移植类库Example: Localized Portable Class Library

下面的本地化可移植类库示例包括法语 (法国) 和英语 (美国) 区域性的资源。The following localized Portable Class Library example includes resources for the French (France) and English (United States) cultures. 英语 (美国) 区域性是应用程序的默认区域性;在表中显示其资源上一节The English (United States) culture is the app's default culture; its resources are shown in the table in the previous section. 法语(法国)区域性的资源文件命名为 LibResources.fr-FR.resx,该文件包含下表中列出的字符串资源。The resources file for the French (France) culture is named LibResources.fr-FR.resx and consists of the string resources listed in the following table. UILibrary 类的源代码与上一部分中所示的相同。The source code for the UILibrary class is the same as that shown in the previous section.

资源名称Resource name 资源值Resource value
BornBorn Date de naissanceDate de naissance
BornLengthBornLength 2020
HiredHired 日期 embauchéDate embauché
HiredLengthHiredLength 1616
IdID IdID
名称Name NomNom
标题Title 基 de données des employésBase de données des employés

以下代码阐释如何从控制台模式应用访问 UILibrary 类及其资源。The following code illustrates how the UILibrary class and its resources can be accessed from a console-mode app. 它需要对 UILibrary.dll 要添加到控制台应用程序项目的引用。It requires a reference to UILibrary.dll to be added to the console app project.

using System;
using System.Collections.Generic;
using System.Globalization;

using MyCompany.Employees;
 
class Program
{
   static void Main(string[] args)
   {
     
      // Get the data from some data source.
      var employees = InitializeData();

      // Display application title.
      string title = UILibrary.GetTitle();
      int start = (Console.WindowWidth + title.Length) / 2;
      string titlefmt = String.Format("{{0,{0}{1}", start, "}");
      Console.WriteLine(titlefmt, title);
      Console.WriteLine();

      // Retrieve resources.
      string[] fields = UILibrary.GetFieldNames();
      int[] lengths = UILibrary.GetFieldLengths();
      string fmtString = String.Empty;
      // Create format string for field headers and data.
      for (int ctr = 0; ctr < fields.Length; ctr++)
         fmtString += String.Format("{{{0},-{1}{2}{3}   ", ctr, lengths[ctr], ctr >= 2 ? ":d" : "", "}");

      // Display the headers.
      Console.WriteLine(fmtString, fields);
      Console.WriteLine();
      // Display the data.
      foreach (var e in employees)
         Console.WriteLine(fmtString, e.Item1, e.Item2, e.Item3, e.Item4);

      Console.ReadLine();
   }

   private static List<Tuple<String, String, DateTime, DateTime>> InitializeData() 
   {
      List<Tuple<String, String, DateTime, DateTime>> employees = new List<Tuple<String, String, DateTime, DateTime>>();
      var t1 = Tuple.Create("John", "16302", new DateTime(1954, 8, 18), new DateTime(2006, 9, 8));
      employees.Add(t1);
      t1 = Tuple.Create("Alice", "19745", new DateTime(1995, 5, 10), new DateTime(2012, 10, 17));
      employees.Add(t1);
      return employees;
   }

}
Imports MyCompany.Employees
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Threading

Module Module1
   Sub Main()
      Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("fr-FR")
      Thread.CurrentThread.CurrentCulture = culture
      Thread.CurrentThread.CurrentUICulture = culture
      Console.WriteLine("Current culture is {0}", CultureInfo.CurrentCulture.Name)

      ' Get the data from some data source.
      Dim employees = InitializeData()

      ' Display application title.
      Dim title As String = UILibrary.GetTitle()
      Dim start As Integer = (Console.WindowWidth + title.Length) \ 2
      Dim titlefmt As String = String.Format("{{0,{0}{1}", start, "}")
      Console.WriteLine(titlefmt, title)
      Console.WriteLine()

      ' Retrieve resources.
      Dim fields() As String = UILibrary.GetFieldNames()
      Dim lengths() As Integer = UILibrary.GetFieldLengths()
      Dim fmtString As String = String.Empty
      ' Create format string for field headers and data.
      For ctr = 0 To fields.Length - 1
         fmtString += String.Format("{{{0},-{1}{2}{3}   ", ctr, lengths(ctr), IIf(ctr >= 2, ":d", ""), "}")
      Next
      ' Display the headers.
      Console.WriteLine(fmtString, fields)
      Console.WriteLine()
      ' Display the data.
      For Each e In employees
         Console.WriteLine(fmtString, e.Item1, e.Item2, e.Item3, e.Item4)
      Next
      Console.ReadLine()
   End Sub

   Private Function InitializeData() As List(Of Tuple(Of String, String, Date, Date))
      Dim employees As New List(Of Tuple(Of String, String, Date, Date))
      Dim t1 = Tuple.Create("John", "16302", #8/18/1954#, #9/8/2006#)
      employees.Add(t1)
      t1 = Tuple.Create("Alice", "19745", #5/10/1995#, #10/17/2012#)
      employees.Add(t1)
      Return employees
   End Function
End Module

下列代码阐释了如何从 UILibrary应用访问 Windows 8.x 应用商店Windows 8.x Store 类及其资源。The following code illustrates how the UILibrary class and its resources can be accessed from a Windows 8.x 应用商店Windows 8.x Store app. 它需要对 UILibrary.dll 要添加到 Windows 应用商店应用项目的引用。It requires a reference to UILibrary.dll to be added to the Windows Store app project. 它使用静态 ApplicationLanguages.PrimaryLanguageOverride 属性将应用的首选语言设置为法语。It uses the static ApplicationLanguages.PrimaryLanguageOverride property to set the app's preferred language to French.

using System;
using System.Collections.Generic;
using Windows.Globalization;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using MyCompany.Employees;

namespace LocConsumerCS
{
    public sealed partial class BlankPage : Page
    {
        public BlankPage()
        {
            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Example.Demo(outputBlock);
        }
    }

    public class Example
    {
        public static void Demo(TextBlock outputBlock)
        {
            // Set the application preferences.
            ApplicationLanguages.PrimaryLanguageOverride = "fr-FR";

            // Get the data from some data source. 
            var employees = InitializeData();
            outputBlock.FontFamily = new FontFamily("Courier New");
            // Display application title.
            string title = UILibrary.GetTitle();
            outputBlock.Text += title + Environment.NewLine + Environment.NewLine; 

            // Retrieve resources.
            string[] fields = UILibrary.GetFieldNames();
            int[] lengths = UILibrary.GetFieldLengths();
            string fmtString = String.Empty;
            // Create format string for field headers and data.
            for (int ctr = 0; ctr < fields.Length; ctr++)
                fmtString += String.Format("{{{0},-{1}{2}{3}   ", ctr, lengths[ctr], ctr >= 2 ? ":d" : "", "}");

            // Display the headers.
            outputBlock.Text += String.Format(fmtString, fields) + Environment.NewLine + Environment.NewLine;

            // Display the data.
            foreach (var e in employees)
                outputBlock.Text += String.Format(fmtString, e.Item1, e.Item2, e.Item3, e.Item4) + Environment.NewLine;
        }

        private static List<Tuple<String, String, DateTime, DateTime>> InitializeData()
        {
            List<Tuple<String, String, DateTime, DateTime>> employees = new List<Tuple<String, String, DateTime, DateTime>>();
            var t1 = Tuple.Create("John", "16302", new DateTime(1954, 8, 18), new DateTime(2006, 9, 8));
            employees.Add(t1);
            t1 = Tuple.Create("Alice", "19745", new DateTime(1995, 5, 10), new DateTime(2012, 10, 17));
            employees.Add(t1);
            return employees;
        }
    }
}
Imports Windows.Globalization
Imports MyCompany.Employees

Public NotInheritable Class BlankPage
    Inherits Page

    Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
        Example.Demo(outputBlock)
    End Sub
End Class

Public Class Example
    Public Shared Sub Demo(outputBlock As Windows.UI.Xaml.Controls.TextBlock)
        ' Set the application preferences.
        ApplicationLanguages.PrimaryLanguageOverride = "fr-FR"

        ' Get the data from some data source. 
        Dim employees = InitializeData()
        outputBlock.FontFamily = New FontFamily("Courier New")
        ' Display application title.
        Dim title As String = UILibrary.GetTitle()
        outputBlock.Text += title + vbCrLf + vbCrLf

        ' Retrieve resources.
        Dim fields() As String = UILibrary.GetFieldNames()
        Dim lengths() As Integer = UILibrary.GetFieldLengths()
        Dim fmtString As String = String.Empty
        ' Create format string for field headers and data.
        For ctr = 0 To fields.Length - 1
            fmtString += String.Format("{{{0},-{1}{2}{3}   ", ctr, lengths(ctr), If(ctr >= 2, ":d", ""), "}")
        Next
        ' Display the headers.
        outputBlock.Text += String.Format(fmtString, fields) + vbCrLf + vbCrLf

        ' Display the data.
        For Each e In employees
            outputBlock.Text += String.Format(fmtString, e.Item1, e.Item2, e.Item3, e.Item4) + vbCrLf
        Next
    End Sub

    Private Shared Function InitializeData() As List(Of Tuple(Of String, String, Date, Date))
        Dim employees As New List(Of Tuple(Of String, String, Date, Date))
        Dim t1 = Tuple.Create("John", "16302", #8/18/1954#, #9/8/2006#)
        employees.Add(t1)
        t1 = Tuple.Create("Alice", "19745", #5/10/1995#, #10/17/2012#)
        employees.Add(t1)
        Return employees
    End Function
End Class

请参阅See also