使用可攜式類別庫的跨平台開發Cross-platform development with the Portable Class Library

在 Visual Studio 中的可攜式類別庫專案類型可協助您快速且輕鬆地建置跨平台應用程式和程式庫,針對 Microsoft 平台。The Portable Class Library project type in Visual Studio helps you build cross-platform apps and libraries for Microsoft platforms quickly and easily.

重要

因為可攜式類別庫專案的目標只有非常特定部分的.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 實作支援特定版本的.NET Standard 為都目標的.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.

可攜式類別庫可幫助您減少開發及測試程式碼的時間和成本。Portable class libraries can help you reduce the time and costs of developing and testing code. 撰寫及建置可攜式.NET Framework 組件,使用這種專案類型,並接著從.NET Framework、 iOS 或 mac 等的多個平台為目標的應用程式中參考這些組件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 the .NET Framework, iOS, or Mac.

即使在 Visual Studio 中建立可攜式類別庫專案並開始開發之後,您還是可以變更目標平台。Even after you create a Portable Class Library project in Visual Studio and start developing it, you can change the target platforms. Visual Studio 會編譯您的程式庫的新的組件,可協助您識別您需要在程式碼中進行的變更。Visual Studio compiles your library with the new assemblies, which helps you identify the changes you need to make in your code.

建立可攜式類別庫專案Create a Portable Class Library project

若要建立可攜式類別庫,請使用 Visual Studio 中提供的範本。To create a Portable Class Library, use the template provided in Visual Studio. 建立新的專案 (檔案 > 新專案),然後在新專案對話方塊方塊中,選取您的程式語言 (Visual C# 或 Visual Basic)。Create a new project (File > New Project), and in the New Project dialog box, select your programming language (Visual C# or Visual Basic). 然後,選取類別庫 (舊版可攜式) 範本。Then, select the Class Library (Legacy Portable) template. 輸入您專案的名稱,然後選擇確定Enter a name for your project and choose OK.

加入可攜式類別庫 對話方塊隨即出現。The Add Portable Class Library dialog box appears. 選擇兩個或多個目標,然後選擇確定Choose two or more targets, and then choose OK.

Visual Studio 中加入可攜式類別程式庫的目標

變更目標Change targets

當您在建立時,或您已經開始開發之後,您可以變更目標平台的可攜式類別庫專案。You can change the target platforms of a portable class library project when you create it or after you’ve started development. 如果您想要建立您的專案,在之後變更目標方案總管,開啟您的可攜式類別庫專案 (而非方案),捷徑功能表,然後選擇屬性.If you want to change the targets after you’ve created your project, in Solution Explorer, open the shortcut menu for your Portable Class Library project (not the solution), and then choose Properties. 在專案屬性頁面上,程式庫 索引標籤上顯示的平台專案的目前目標。On the project properties page, the Library tab shows the platforms that your project currently targets.

在 Visual Studio 中的可攜式類別庫的專案屬性

若要新增或移除目標,選擇變更按鈕,然後選取及清除適當的核取方塊。To add or remove targets, choose the Change button, and then select and clear the appropriate check boxes.

當您變更目標時,可供您用來開發專案的 API 會變更,以配合您的選項。When you change the targets, the APIs that are available to you for developing your project will change to match your selection. Visual Studio 會提報因為目標變更而可能發生的錯誤和警告。Visual Studio reports the errors and warnings that may occur as a result of the targets changing.

如果您想要評估的可攜性的組件之前,請在 Visual Studio 中進行變更,您可以使用.NET Portability AnalyzerIf you want to evaluate the portability of your assemblies before you make changes in Visual Studio, you can use the .NET Portability Analyzer.

支援的類型和成員Supported types and members

可攜式類別庫專案中提供的類型和成員受到多個相容性因素所限制:The types and members that are available in Portable Class Library projects are constrained by several compatibility factors:

  • 它們必須在您選取的目標之間共用。They must be shared across the targets you selected.

  • 它們必須在這些目標上具有類似的行為。The must behave similarly across those targets.

  • 它們不能是要被取代的候選項。They must not be candidates for deprecation.

  • 它們在可攜式環境中必須是合理的,尤其是支援成員無法移植時。They must make sense in a portable environment, especially when supporting members are not portable.

如果可攜式類別庫和您選取的目標可支援某成員,該成員就會出現在 IntelliSense 的專案中。If a member is supported in the Portable Class Library and for your selected targets, it will appear in your project in IntelliSense. 不過,請記得,可攜式類別庫可能會支援 API,但您是否可以使用 API,取決於您選取的目標。However, remember that an API may be supported in the Portable Class Library, but whether you can use the API depends on the targets you select.

可攜式類別庫中的 API 差異API differences in the Portable Class Library

為了讓可攜式類別庫組件在所有支援的平台上都相容,可攜式類別庫中有部分成員已稍微變更。To make Portable Class Library assemblies compatible across all supported platforms, some members have been slightly changed in the Portable Class Library.

使用可攜式類別庫Use the Portable Class Library

建立可攜式類別庫專案之後,即可從其他專案參考該專案。After you build your Portable Class Library project, you just reference it from other projects. 您可以參考此專案,或是包含您想要存取之類別的特定組件。You can reference either the project or specific assemblies that contain the classes you want to access.

若要執行參考可攜式類別庫組件的應用程式,電腦上必須安裝目標平台所需的版本 (或以後版本)。To run an app that references a Portable Class Library assembly, the required version (or later) of the targeted platforms must be installed on your computer. Visual Studio 包含所有必要架構,因此不需要進一步修改用來開發應用程式的電腦,即可執行該應用程式。Visual Studio contains all the required frameworks, so you can run the app without further modification on the computer that you used to develop the app.

將通用 Windows 應用程式部署Deploy a Universal Windows app

當您建立通用 Windows 應用程式參考可攜式類別庫組件,您要部署應用程式的所有一切皆內含在應用程式套件中,並不需要任何進一步的步驟。When you create a Universal Windows app that references a Portable Class Library assembly, everything you need to deploy the app is included in the app package, and no further steps are required.

部署.NET Framework 應用程式Deploy a .NET Framework app

當您部署參考可攜式類別庫組件的 .NET Framework 應用程式時,必須指定正確 .NET Framework 版本的相依性。When you deploy a .NET Framework app that references a Portable Class Library assembly, you must specify a dependency on the correct version of the .NET Framework. 藉由指定此相依性,您就可以確保所需的版本會隨著您的應用程式一起安裝。By specifying this dependency, you ensure that the required version is installed with your app.

  • 若要建立與 ClickOnce 部署的相依性:在 [方案總管] 中,選擇您想要發佈專案的專案節點。To create a dependency with ClickOnce deployment: In Solution Explorer, choose the project node for the project you want to publish. (這是參考可攜式類別庫專案的專案。)在功能表列上選擇 專案 > 屬性,然後選擇發佈 索引標籤。在 發佈頁面上,選擇必要條件(This is the project that references the Portable Class Library project.) On the menu bar, choose Project > Properties, and then choose the Publish tab. On the Publish page, choose Prerequisites. 選取所需的 .NET Framework 版本做為必要條件。Select the required .NET Framework version as a prerequisite.

  • 若要建立安裝專案的相依性:在 方案總管 中,選擇 安裝專案。To create a dependency with a setup project: In Solution Explorer, choose the setup project. 在功能表列上選擇 專案 > 屬性 > 必要條件On the menu bar, choose Project > Properties > Prerequisites. 選取所需的 .NET Framework 版本做為必要條件。Select the required .NET Framework version as a prerequisite.

如需部署.NET Framework 應用程式的詳細資訊,請參閱 < 開發人員部署手冊For more information about deploying .NET Framework apps, see Deployment Guide for Developers.

另請參閱See also