Dotnet Core 简明教程
.NET Core - Portable Class Library
在本章中,我们将讨论什么是PCL(可移植类库),以及为什么我们需要PCL。为了理解此概念,让我们打开上一章中创建的类库项目文件夹。
In this chapter, we will discuss what is PCL (Portable Class Library), and also why we need PCL. To understand this concept, let us open the class library project folder which we have created in the previous chapter.
在该文件夹中,您会看到除了project.json和CS文件之外,我们还有一个*.xproj文件,原因在于Visual Studio将.NET Core项目类型设置为*.xproj,而不是*.csproj。
In this folder, you can see that in addition to project.json and CS files we also have *.xproj file, and that is because Visual Studio setup .NET Core project type as *.xproj instead of *.csproj.
如Microsoft指出的那样,.xproj将逐渐消失,但它仍保留在预览2版本工具中。正如我们所介绍的,UWP应用程序使用.csproj。
As mentioned by Microsoft, *.xproj will be going away, but it is still here in preview 2 tooling. As we have covered that UWP application uses the *.csproj.
现在实际上不可行的是引用*.xproj,而*.csproj也不会实现该功能,因为*.xproj将被淘汰。
Now it is actually not feasible to get *.csproj to reference and *.xproj and that functionality is not going to be implemented because *.xproj will move out.
所以,我们需要的是可以在控制台应用程序和UWP应用程序之间共享的类库,而PCL正是如此。
So instead, we need a class library which can be shared between the console app and the UWP app and here comes PCL.
What is PCL
现在让我们了解PCL是什么−
Let us now understand what PCL is −
-
The Portable Class Library project enables you to write and build managed assemblies that work on more than one .NET Framework platform.
-
You can create classes that contain code you wish to share across many projects, such as shared business logic, and then reference those classes from different types of projects.
-
It can also help you build cross-platform apps and libraries for Microsoft platforms quickly and easily.
-
Portable class libraries can help you reduce the time and costs of developing and testing code.
-
Use this project type to write and build portable .NET Framework assemblies, and then reference those assemblies from apps that target multiple platforms such as Windows and Windows Phone, etc.
让我们现在从“解决方案资源管理器”中删除我们创建的类库。同时,从解决方案文件夹中将其删除,并进一步添加一个新项目项。
Let us now remove the class library which we have created from the Solution Explorer. At the same time, delete it from the Solution folder and further add a new project item.
在左窗格中选择 Visual C# → Windows 模板,在中间窗格中选择“类库(可移植)”。
Select the Visual C# → Windows template in the left pane and select Class Library (Portable) in the middle pane.
在名称字段中输入 StringLibrary,然后单击 OK 以创建此项目。
Enter StringLibrary in the name field and click OK to create this project.
现在,我们需要选择要引用的目标框架。让我们暂时选择 Windows Universal 和 ASP.NET Core,然后我们将重新定位它。单击 OK 。
Now we need to select the target frameworks to reference. Let us select Windows Universal and ASP.NET Core for a moment then we will retarget it. Click OK.
您会看到它已按 PCF 格式创建了一个新项目。让我们现在在“解决方案资源管理器”中右键单击 StringLibrary 项目并选择“属性”。
You can see that it has created a new project in PCF format. Let us now right-click StringLibrary project in the Solution Explorer and select Properties.
单击“目标 .NET 平台标准”。
Click on the Target .NET Platform Standard.
单击”是“;现在与一个细微差别相同的类库。不同之处在于它也可以被 UWP 使用,因为它包含 *.csproj 文件,而不是 *.xproj。
Click Yes; it is now the same class library with one minor difference. The difference is that it can be used by UWP as well, because it contains *.csproj file instead of *.xproj.
现在,让我们添加一个新类;为此,您需要右键单击“解决方案资源管理器”中的项目并选择 Add → Class…
Let us now add a new class; for this, you need to right-click on project in Solution Explorer and select Add → Class…
在中间窗格中选择“类”,在名称字段中输入 StringLib.cs ,然后单击 Add 。添加类后,替换 StringLib.cs 文件中的以下代码。
Select class in the middle pane and enter StringLib.cs in the name field and then Click Add. Once the class is added, then replace the following code in StringLib.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StringLibrary {
public static class StringLib {
public static bool StartsWithUpper(this String str) {
if (String.IsNullOrWhiteSpace(str))
return false;
Char ch = str[0];
return Char.IsUpper(ch);
}
public static bool StartsWithLower(this String str) {
if (String.IsNullOrWhiteSpace(str))
return false;
Char ch = str[0];
return Char.IsLower(ch);
}
public static bool StartsWithNumber(this String str) {
if (String.IsNullOrWhiteSpace(str))
return false;
Char ch = str[0];
return Char.IsNumber(ch);
}
}
}
让我们构建这个可移植类库项目,它应该在没有错误的情况下编译。现在,我们需要在我们的控制台项目中添加这个可移植类库的引用。因此,展开 FirstApp,右键单击“引用”,然后选择 Add Reference…
Let us build this portable class library project and it should compile without error. Now we need to add reference of this portable class library in our console project. So, expand FirstApp and right-click on References and select Add Reference…
在“引用管理器”对话框中,选择我们可移植类库项目的 StringLibrary,然后单击 OK 。
In the Reference Manager dialog box, select StringLibrary which is our portable class library project, and then click OK.
您会看到 StringLibrary 引用已添加到控制台项目中,而且在 project.json 文件中也可以看到。
You can see that the StringLibrary reference is added to the console project and it can be seen in the project.json file as well.
现在,您可以再次运行应用程序,您将看到相同的输出。
You can now run the application again and you will see the same output.
现在让我们在您的项目中使用可移植类库的其他扩展方法。同一个可移植库也将被您的 UWP 应用程序使用。
Let us now use the other extension methods of your portable class library in your project. The same portable library will be consumed in your UWP application as well.