Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to package a .NET library targeting the Universal Windows Platform?

How do I package a Universal Windows Platform library in a modern general-purpose way for publishing via NuGet? Let's assume I have a single AnyCPU assembly written in C# that exports some code and XAML user controls.

This is a series of questions and answers that document my findings on the topic of modern NuGet package authoring, focusing especially on the changes introduced with NuGet 3. You may also be interested in some related questions:

  • How to package a .NET Framework library?
  • How to package a portable .NET library targeting .NET Core?
  • How to package a .NET library targeting .NET Framework and Universal Windows Platform and include platform-specific functionality?
  • How to package a multi-architecture .NET library that targets the Universal Windows Platform?
  • How to package a .NET library that targets the Universal Windows Platform and depends on Visual Studio extension SDKs?
like image 494
Sander Avatar asked Jan 05 '16 12:01

Sander


People also ask

What is UWP NuGet packages?

17 Aug 20216 minutes to read. NuGet can be used to automatically add files and references to your Visual Studio projects. You can use the Syncfusion UWP NuGet packages without installing the Essential Studio or UWP platform installation to development with the Syncfusion UWP controls.

Does UWP use .NET framework?

NET Standard is supported in which UWP versions. If you are developing a Desktop app, see instead . NET Framework versions and dependencies for detailed information on .

How do I target multiple net frameworks?

To target multiple frameworks, change <TargetFramework> to plural <TargetFrameworks> and include monikers for different frameworks you want to target separated by ; . Here, we will support two more frameworks . NET Framework 4.0 & 4.6. So include net40 and net46 monikers respectively as shown below.


1 Answers

This answer builds upon the principles used to package libraries targeting the .NET Framework. Read the linked answer first to better understand the following.

First you must check the "Generate library layout" checkbox in the project build settings. Without this, it will not be possible to use the XAML user controls exported by your library. Make sure to apply this setting for all build configurations and architectures.

In addition to the basic 3 assets (see answer linked above), you will also need to package the following additional assets from your build output directory:

  • MyUwpLibrary.pri
  • MyUwpLibrary subdirectory

These extra resources are required to make use of XAML user controls exported by your assembly. Always include these assets, even if you do not yet export any XAML from your library - you might do it one day and this will be tricky to remember later on!

To publish the UWP library, you need to create a NuGet package with the following structure:

\---lib
    \---uap10.0
        |   MyUwpLibrary.dll
        |   MyUwpLibrary.pdb
        |   MyUwpLibrary.pri
        |   MyUwpLibrary.xml
        |
        \---MyUwpLibrary
                HelloWorld.xaml
                MyUwpLibrary.xr.xml

If you are familiar with .NET Framework library publishing, this should look quite familiar and straightforward. You can use the following template for your nuspec file:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata minClientVersion="3.2">
        <id>Example.MyUwpLibrary</id>
        <version>1.0.0</version>
        <authors>Firstname Lastname</authors>
        <description>Example of a UWP library that exports a user control.</description>
        <dependencies>
            <dependency id="Newtonsoft.Json" version="8.0.1" />
        </dependencies>
    </metadata>
    <files>
        <!-- The double wildcard will also grab all the resource files and the resource subdirectory. -->
        <file src="..\bin\Release\MyUwpLibrary**" target="lib\uap10.0" />
    </files>
</package>

That's it, really! Remember to build your solution using the Release configuration before creating the NuGet package. For more details, refer to the answer about packaging a .NET Framework library, linked above.

A sample library and the relevant packaging files are available on GitHub. The solution corresponding to this answer is SimpleUwpLibrary.

like image 158
Sander Avatar answered Oct 25 '22 12:10

Sander