2016-01-05 53 views
3

如何以現代通用的方式打包具有以下屬性的.NET庫?如何打包面向.NET Framework和Universal Windows Platform的.NET庫幷包含特定於平臺的功能?

  • 爲.NET Framework 4.6和通用Windows平臺提供了一些共享功能。
  • 提供了一些特定於平臺的功能來與外部庫電位平臺特定的依賴關係的每個(例如,專門的類或API,包括用於UWP XAML用戶控件)。
  • 是架構無關(AnyCPU)。
  • 其中所述的便攜式子集可通過靶向一個兼容的API表面其它便攜式庫一起使用。

這是一系列的問題,並且記錄在現代NuGet包創作的主題我的發現,特別是集中在與的NuGet 3.引入的變化也可能對一些相關問題的答案:

回答

2

這個答案建立在principles used to package .NET Framework libraries,該principles used to package Universal Windows Platform librariesprinciples used to package portable libraries。首先閱讀鏈接的答案以更好地理解以下內容。

爲了服務於描述集的平臺,你需要相應地構建解決方案爲多級庫項目:

  • 特定於平臺的類庫的.NET Framework 4.6。
  • 特定於平臺的類庫通用Windows平臺。
  • 一款面向公共API表面的便攜式庫。

您將要達到以下NuGet包結構:

\---lib 
    +---dotnet 
    |  MyPortableLibrary.dll 
    |  MyPortableLibrary.pdb 
    |  MyPortableLibrary.XML 
    | 
    +---net46 
    |  MyDotNetLibrary.dll 
    |  MyDotNetLibrary.pdb 
    |  MyDotNetLibrary.XML 
    |  MyPortableLibrary.dll 
    |  MyPortableLibrary.pdb 
    |  MyPortableLibrary.XML 
    | 
    \---uap10.0 
     | MyPortableLibrary.dll 
     | MyPortableLibrary.pdb 
     | MyPortableLibrary.XML 
     | MyUwpLibrary.dll 
     | MyUwpLibrary.pdb 
     | MyUwpLibrary.pri 
     | MyUwpLibrary.XML 
     | 
     \---MyUwpLibrary 
       HashControl.xaml 
       MyUwpLibrary.xr.xml 

如果你已經熟悉了上面鏈接其他的答案,這似乎應該比較熟悉。唯一特別的部分是便攜式圖書館存在三個副本,因爲其內容將提供給所有目標平臺。

<?xml version="1.0"?> 
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> 
    <metadata minClientVersion="3.2"> 
     <id>Example.MyMultiSurfaceLibrary</id> 
     <version>1.0.0</version> 
     <authors>Firstname Lastname</authors> 
     <description>Example of a multi-platform library that exposes different API surfaces to .NET 4.6 and UWP and also includes a portable component.</description> 
     <dependencies> 
      <!-- UWP has more dependencies than other platforms (Newtonsoft.Json). --> 
      <group targetFramework="uap10.0"> 
       <dependency id="Newtonsoft.Json" version="8.0.1" /> 

       <dependency id="System.Linq" version="4.0.0" /> 
       <dependency id="System.Numerics.Vectors" version="4.1.0" /> 
       <dependency id="System.Resources.ResourceManager" version="4.0.0" /> 
       <dependency id="System.Runtime" version="4.0.20" /> 
      </group> 

      <!-- All other platforms - just the dependencies of the portable library here. --> 
      <group> 
       <dependency id="System.Linq" version="4.0.0" /> 
       <dependency id="System.Numerics.Vectors" version="4.1.0" /> 
       <dependency id="System.Resources.ResourceManager" version="4.0.0" /> 
       <dependency id="System.Runtime" version="4.0.20" /> 
      </group> 
     </dependencies> 
    </metadata> 
    <files> 
     <file src="..\bin\Release\MyPortableLibrary.*" target="lib\net46" /> 
     <file src="..\bin\Release\MyPortableLibrary.*" target="lib\uap10.0" /> 
     <file src="..\bin\Release\MyPortableLibrary.*" target="lib\dotnet" /> 

     <file src="..\..\MyDotNetLibrary\bin\Release\MyDotNetLibrary.*" target="lib\net46" /> 

     <!-- Double wildcard also ensures that the subdirectory is packaged. --> 
     <file src="..\..\MyUwpLibrary\bin\Release\MyUwpLibrary**" target="lib\uap10.0" /> 
    </files> 
</package> 

注意,定義兩個不同的依賴關係的集合:一個通用基團和一個特定於通用視窗

所需封裝結構可以通過使用基於以下模板nuspec文件來實現平臺,因爲UWP庫對Newtonsoft.Json軟件包有額外的依賴性。您可以將相同模式擴展到具有特定於平臺的相關性的任意數量的平臺。

就是這樣 - 這個NuGet包現在可以安裝到.NET Framework 4.6項目,通用Windows平臺項目和麪向兼容API表面的可移植庫項目中。便攜式庫的功能將在所有平臺上導出,而平臺特定的庫也在適當的平臺上使用。

記得在創建NuGet包之前使用Release配置來構建您的解決方案。

樣本庫和相關打包文件是available on GitHub。與此答案相對應的解決方案是MultiSurfaceLibrary。

相關問題