使用 Visual Studio 2017 或 2019 建立 Xamarin 的套件

適用於 Xamarin 的套件包含使用 iOS、Android 和 Windows (視執行階段作業系統而定) 上原生 API 的程式碼。 雖然這十分簡單,但最好是讓開發人員使用 PCL 中的套件,或透過通用 API 介面區的 .NET Standard 程式庫。

在本逐步解說中,您會使用 Visual Studio 2017 或 2019 來建立可在 iOS、Android 和 Windows 上行動專案中使用的跨平臺NuGet套件。

  1. 先決條件
  2. 建立專案結構和抽象程式碼
  3. 撰寫平台特定程式碼
  4. 建立和更新 .nuspec 檔案
  5. 封裝元件
  6. 相關主題

必要條件

  1. Visual Studio 2017 或 2019 通用 Windows 平臺 (UWP) 和 Xamarin。 從 visualstudio.com 免費安裝 Community Edition,當然也可以使用 Professional Edition 和 Enterprise Edition。 若要包含 UWP 和 Xamarin 工具,請選取 [自訂安裝] 並檢查適當的選項。
  2. NuGet CLI。 從 nuget.org/downloads 下載最新版的 nuget.exe,並將它儲存至您選擇的位置。 如果尚未新增,則請將該位置新增至您的 PATH 環境變數。

注意

nuget.exe 本身是 CLI 工具,不是安裝程式,因此請務必從瀏覽器儲存下載的檔案,而不是執行它。

建立專案結構和抽象程式碼

  1. 下載並執行適用于 Visual Studio 的跨平臺 .NET Standard 外掛程式範本延伸模組。 這些範本可讓您輕鬆建立此逐步解說的必要專案結構。

  2. 在 Visual Studio 2017 中,檔案 > 新增 > Project,搜尋 Plugin ,選取[跨平臺 .NET 標準程式庫外掛程式] 範本,將名稱變更為 LoggingLibrary,然後按一下 [確定]。

    New Blank App (Xamarin.Forms Portable) project in VS 2017

    在 Visual Studio 2019 中,[檔案 > 新增 > ] Project,搜尋 Plugin ,選取[跨平臺 .NET 標準程式庫外掛程式]範本,然後按 [下一步]。

    New Blank App (Xamarin.Forms Portable) project in VS 2019

    將名稱變更為 LoggingLibrary,然後按一下 [建立]。

    New Blank App (Xamarin.Forms Portable) configuration in VS 2019

產生的解決方案包含兩個共用專案,以及各種平臺特定專案:

  • 包含在 ILoggingLibrary 檔案中的 ILoggingLibrary.shared.cs 專案會定義公用介面, (元件的 API 介面區) 。 這是您在其中定義程式庫介面的位置。
  • 另一個共用專案包含 中的 CrossLoggingLibrary.shared.cs 程式碼,會在執行時間找出抽象介面的平臺特定實作。 您通常不需要修改這個檔案。
  • 平臺特定專案,例如 LoggingLibrary.android.cs ,每個專案都包含介面的原生實作,其各自 LoggingLibraryImplementation.cs (VS 2017) 或 LoggingLibrary.<PLATFORM>.cs (VS 2019) 檔案。 這是您建置程式庫程式碼的位置。

根據預設,專案的 ILoggingLibrary.shared.cs 檔案 ILoggingLibrary 包含介面定義,但沒有任何方法。 基於本逐步解說的目的,新增 Log 方法,如下所示:

using System;
using System.Collections.Generic;
using System.Text;

namespace Plugin.LoggingLibrary
{
    /// <summary>
    /// Interface for LoggingLibrary
    /// </summary>
    public interface ILoggingLibrary
    {
        /// <summary>
        /// Log a message
        /// </summary>
        void Log(string text);
    }
}

撰寫平台特定程式碼

若要實作 ILoggingLibrary 介面的平台特定實作和其方法,請執行下列動作:

  1. LoggingLibraryImplementation.cs開啟每個平臺專案的 (VS 2017) 或 LoggingLibrary.<PLATFORM>.cs (VS 2019) 檔案,並新增必要的程式碼。 例如,使用 Android 平臺專案) (:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Plugin.LoggingLibrary
    {
        /// <summary>
        /// Implementation for Feature
        /// </summary>
        public class LoggingLibraryImplementation : ILoggingLibrary
        {
            /// <summary>
            /// Log a message
            /// </summary>
            public void Log(string text)
            {
                throw new NotImplementedException("Called Log on Android");
            }
        }
    }
    
  2. 針對每個您想要支援的平台,重複專案中的這個實作。

  3. 以滑鼠右鍵按一下方案,然後選取 [建置方案] 檢查您的工作,並產生之後將封裝的成品。 如果您收到有關遺漏參考的錯誤,請以滑鼠右鍵按一下方案,然後選取 [還原 NuGet 套件] 以安裝相依性並重建。

注意

如果您使用 Visual Studio 2019,在選取 [還原NuGet套件並嘗試重建之前,您必須在 中 LoggingLibrary.csproj 將 的版本 MSBuild.Sdk.Extras 變更為 2.0.54 。 此檔案只能先以滑鼠右鍵按一下) 方案下方的專案 (,然後選取 ,然後選取 ,然後以滑鼠右鍵按一下卸載的專案,然後選取 Unload ProjectEdit LoggingLibrary.csproj

注意

若要針對 iOS 建置,您需要連線至 Visual Studio 的網路 Mac,如 Xamarin.iOS for Visual Studio 簡介上所述。 如果您沒有可用的 Mac,請在組態管理員中清除 iOS 專案 (上述步驟 3)。

建立和更新 .nuspec 檔案

  1. 開啟命令提示字元,並巡覽至 LoggingLibrary 資料夾 (低於 .sln 檔案一階),然後執行 NuGet spec 命令以建立初始的 Package.nuspec 檔案:

    nuget spec
    
  2. 將這個檔案重新命名為 LoggingLibrary.nuspec,並在編輯器中予以開啟。

  3. 更新檔案使其符合下列內容,並使用適當的值取代 YOUR_NAME。 尤其是在整個 nuget.org 中,<id> 值必須是唯一的 (請參閱建立套件中所述的命名慣例。 另請注意,您也必須更新作者和描述標記,否則會在封裝步驟期間發生錯誤。

    <?xml version="1.0"?>
    <package >
        <metadata>
        <id>LoggingLibrary.YOUR_NAME</id>
        <version>1.0.0</version>
        <title>LoggingLibrary</title>
        <authors>YOUR_NAME</authors>
        <owners>YOUR_NAME</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Awesome application logging utility</description>
        <releaseNotes>First release</releaseNotes>
        <copyright>Copyright 2018</copyright>
        <tags>logger logging logs</tags>
        </metadata>
    </package>
    

提示

您的套件版本可以加上 -alpha-beta-rc 後置詞,以將套件標示為發行前版本,如需發行前版本的詳細資訊,請檢查發行前版本

新增參考組件

若要包含平台特定參考組件,請針對您的支援平台適當地將下列新增至 LoggingLibrary.nuspec<files> 項目:

<!-- Insert below <metadata> element -->
<files>
    <!-- Cross-platform reference assemblies -->
    <file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.dll" />
    <file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.xml" />
    <file src="Plugin.LoggingLibrary.Abstractions\bin\Release\Plugin.LoggingLibrary.Abstractions.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.Abstractions.dll" />
    <file src="Plugin.LoggingLibrary.Abstractions\bin\Release\Plugin.LoggingLibrary.Abstractions.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.Abstractions.xml" />

    <!-- iOS reference assemblies -->
    <file src="Plugin.LoggingLibrary.iOS\bin\Release\Plugin.LoggingLibrary.dll" target="lib\Xamarin.iOS10\Plugin.LoggingLibrary.dll" />
    <file src="Plugin.LoggingLibrary.iOS\bin\Release\Plugin.LoggingLibrary.xml" target="lib\Xamarin.iOS10\Plugin.LoggingLibrary.xml" />

    <!-- Android reference assemblies -->
    <file src="Plugin.LoggingLibrary.Android\bin\Release\Plugin.LoggingLibrary.dll" target="lib\MonoAndroid10\Plugin.LoggingLibrary.dll" />
    <file src="Plugin.LoggingLibrary.Android\bin\Release\Plugin.LoggingLibrary.xml" target="lib\MonoAndroid10\Plugin.LoggingLibrary.xml" />

    <!-- UWP reference assemblies -->
    <file src="Plugin.LoggingLibrary.UWP\bin\Release\Plugin.LoggingLibrary.dll" target="lib\UAP10\Plugin.LoggingLibrary.dll" />
    <file src="Plugin.LoggingLibrary.UWP\bin\Release\Plugin.LoggingLibrary.xml" target="lib\UAP10\Plugin.LoggingLibrary.xml" />
</files>

注意

若要縮短 DLL 和 XML 檔案的名稱,請以滑鼠右鍵按一下任何指定的專案,並選取 [程式庫] 索引標籤,然後變更組件名稱。

新增相依性

如果您有原生實作的特定相依性,請搭配使用 <dependencies> 項目與 <group> 項目來指定它們,例如:

<!-- Insert within the <metadata> element -->
<dependencies>
    <group targetFramework="MonoAndroid">
        <!--MonoAndroid dependencies go here-->
    </group>
    <group targetFramework="Xamarin.iOS10">
        <!--Xamarin.iOS10 dependencies go here-->
    </group>
    <group targetFramework="uap">
        <!--uap dependencies go here-->
    </group>
</dependencies>

例如,下列會將 iTextSharp 設定為 UAP 目標的相依性:

<dependencies>
    <group targetFramework="uap">
        <dependency id="iTextSharp" version="5.5.9" />
    </group>
</dependencies>

最終 .nuspec

最終 .nuspec 檔案現在看起來應該如下所示,並且應該將 YOUR_NAME 取代為適當值:

<?xml version="1.0"?>
<package >
    <metadata>
    <id>LoggingLibrary.YOUR_NAME</id>
    <version>1.0.0</version>
    <title>LoggingLibrary</title>
    <authors>YOUR_NAME</authors>
    <owners>YOUR_NAME</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Awesome application logging utility</description>
    <releaseNotes>First release</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>logger logging logs</tags>
        <dependencies>
        <group targetFramework="MonoAndroid">
            <!--MonoAndroid dependencies go here-->
        </group>
        <group targetFramework="Xamarin.iOS10">
            <!--Xamarin.iOS10 dependencies go here-->
        </group>
        <group targetFramework="uap">
            <dependency id="iTextSharp" version="5.5.9" />
        </group>
    </dependencies>
    </metadata>
    <files>
        <!-- Cross-platform reference assemblies -->
        <file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.dll" />
        <file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.xml" />
        <file src="Plugin.LoggingLibrary.Abstractions\bin\Release\Plugin.LoggingLibrary.Abstractions.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.Abstractions.dll" />
        <file src="Plugin.LoggingLibrary.Abstractions\bin\Release\Plugin.LoggingLibrary.Abstractions.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.Abstractions.xml" />

        <!-- iOS reference assemblies -->
        <file src="Plugin.LoggingLibrary.iOS\bin\Release\Plugin.LoggingLibrary.dll" target="lib\Xamarin.iOS10\Plugin.LoggingLibrary.dll" />
        <file src="Plugin.LoggingLibrary.iOS\bin\Release\Plugin.LoggingLibrary.xml" target="lib\Xamarin.iOS10\Plugin.LoggingLibrary.xml" />

        <!-- Android reference assemblies -->
        <file src="Plugin.LoggingLibrary.Android\bin\Release\Plugin.LoggingLibrary.dll" target="lib\MonoAndroid10\Plugin.LoggingLibrary.dll" />
        <file src="Plugin.LoggingLibrary.Android\bin\Release\Plugin.LoggingLibrary.xml" target="lib\MonoAndroid10\Plugin.LoggingLibrary.xml" />

        <!-- UWP reference assemblies -->
        <file src="Plugin.LoggingLibrary.UWP\bin\Release\Plugin.LoggingLibrary.dll" target="lib\UAP10\Plugin.LoggingLibrary.dll" />
        <file src="Plugin.LoggingLibrary.UWP\bin\Release\Plugin.LoggingLibrary.xml" target="lib\UAP10\Plugin.LoggingLibrary.xml" />
    </files>
</package>

封裝元件

只要已完成的 .nuspec 參考您需要包含在套件中的所有檔案,就可隨時執行 pack 命令:

nuget pack LoggingLibrary.nuspec

這將產生 LoggingLibrary.YOUR_NAME.1.0.0.nupkg。 在如 NuGet 套件總管的工具中開啟此檔案,並展開所有節點,您將會看到下列內容:

NuGet Package Explorer showing the LoggingLibrary package

提示

.nupkg 檔案只是一個使用不同副檔名的 ZIP 檔。 然後,您也可以將 .nupkg 變更為 .zip 來檢查套件內容,但是請記住要先還原副檔名,再將套件上傳至 nuget.org。

若要讓您的套件可供其他開發人員使用,請遵循 發佈套件的指示。