简体   繁体   中英

How do I distribute a font in a nuget package using the new csproj approach to create nuget packages?

I have a nuget package for Xamarin Forms (ios and android) with which I want to distribute a custom font. I can't find from the nuget documentation how I can include the font in the nuget package so that it gets added to the iOS or Android project that consumes the package. Is this even possible? If so, how?

How do I distribute a font in a nuget package using the new csproj approach to create nuget packages?

To include the font in the nuget package and use it in the new csproj iOS or Android project, you should use contentFiles to include the font file in the .nuspec file, like:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata>
    <id>MyFontPackage</id>
    <version>1.0.0</version>
    <authors>Tester</authors>
    <owners>Tester</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package Description</description>
    <contentFiles>
      <files include="any/any/Fonts/xx.ttf" buildAction="content" flatten="true" copyToOutput="false"/>
    </contentFiles>
  </metadata>

  <files>
    <file src="contentFiles/any/any/Fonts/xx.ttf" target="contentFiles/any/any/Fonts" />
  </files>
</package>

Check the document NuGet ContentFiles Demystified and another thread for some more details.

Update:

If you are creating the nuget package directly from the new sdk style csproj, you can add following in your .csproj, then re-create the package, the font file will be added to the nuget package:

  <ItemGroup>
    <None Include="font\test.ttf" Pack="true"/>
  </ItemGroup>

This file added as content file:

在此处输入图片说明

Check the document NuGet pack and restore as MSBuild targets for some more details.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM