Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly accessed via NuGet package doesn't include a class

A Universal Class Library (UWP) project that I created has two classes;

1. A public BoolToVisibilityConverter - an IValueConverter whose responsibility it is to convert a bool to a XAML Visibility, and vice-versa.

2. A public CustomControl templated control whose template (defined in Themes\Generic.xaml (project structure below)) uses the BoolToVisibilityConverter mentioned above.

Its template looks like this;

<Border ...>
    <Border.Resources>
        <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
    </Border.Resources>
    <Grid ...>
        <Grid ...
            Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Converter={StaticResource BoolToVisibilityConverter}}" />
    </Grid>
</Border>

Value, the property the Visibility is bound to, is a boolean dependency property on the control, local being an alias to the namespace within which the BoolToVisibilityConverter resides (which is the same as that of the templated control).

Project Structure

Project structure

Build Configurations

The Release build configuration of the project has the "XML documentation file" and "Generate library layout" options enabled.

Build configuration

Generating a NuGet package

The NuSpec for my project (located at NuGet\RefClassLibrary.nuspec) is;

<?xml version="1.0"?>
<package >
    <metadata>
        <id>RefClassLibrary</id>
        <version>1.0.0</version>
        <!-- various other metadata -->
    </metadata>
    <files>
        <file src="..\bin\Release\**" target="lib\uap10.0"/>
    </files>
</package>

The file was generated using the nuget.exe spec command, with the NuGet CLI version 4.3.0.4406.

To generate the NuGet package, I first deleted the bin and obj folders, and built the project in the Release (Any CPU) configuration, which generated the following structure in the bin folder,

Bin structure

I then generated the NuGet package for the class library using the command nuget.exe pack RefClassLibrary.nuspec from within the NuGet\ folder, which generated a nupkg file with the following structure,

nupkg structure

Problem: Consuming the class library via the NuGet package

From within a new Universal Windows Application project I ran the Install-Package commmand with the absolute path to the nupkg as its argument, within the Package Manager Console within Visual Studio, which installed the generated package.

After building the project (without errors), I added the following XAML to a new Blank Page,

<Page ...
    xmlns:rcl="using:RefClassLibrary">
    <Grid ...>
        <rcl:CustomControl Width="200" Height="200" Value="True"/>
    </Grid>
</Page>

Which resulted in an error that reads,

XamlParseException: The text associated with this error code could not be found.

Cannot deserialize XBF metadata type list as 'BoolToVisibilityConverter' was not found in namespace 'RefClassLibrary'. [Line: 0 Position: 0]

StackTrace:
at Windows.UI.Xaml.Controls.Control.ApplyTemplate()
at Microsoft.VisualStudio.DesignTools.UwpDesigner.InstanceBuilders.WindowsUIXamlLocalInstanceManager.EnsureElementInDictionary(Object root, ViewNode knownAncestor)

InnerException: None

error

Upon viewing RefClassLibrary in the Object Browser, sure enough as the error suggests, the BoolToVisibilityConverter class wasn't there...

object browser

Consuming the class library directly

I used the Uninstall-Package RefClassLibrary command to remove the package from the project.

I then unzipped the nupkg file using the command 7z x RefClassLibrary.1.0.0.nupkg -oOut, which extracts it to a folder Out, and directly referenced the dll contained within the Out\lib\uap10.0\ folder within the project by adding a Reference via Solution Explorer, which made it work perfectly. The BoolToVisibilityConverter did show up in the Object Browser.

How should this issue be fixed?

The correct assembly gets included within the NuGet package, although some parts of it (?) don't get 'imported' into the project...

like image 941
ravindUwU Avatar asked Sep 18 '25 03:09

ravindUwU


1 Answers

When a NuGet package is installed via the Install-Package Cmdlet, this is, as I understand, what NuGet does,

  1. The identifier and version number of the package to be installed are retrieved. It does so via the specified arguments (in the case of a version number not being specified, it defaults to the latest 'stable' version), or by reading the contents of the package that it points to.

  2. The .nupkg file and some other assets associated with the package are copied to and cached within the %USERPROFILE%\.nuget\packages\{identifier}\{version}\ folder.

  3. The package is then extracted and installed within the Visual Studio project that you are in.

When you install the same version of the same package again, NuGet uses the cached files located within the aforementioned folder, instead of re-downloading them.


The problem described in the question originated as a result of me not incrementing the version number of the NuGet package.

I developed the library project in two steps on my machine,

  1. I intially created the library project with only the CustomControl, generated the library package, and installed it in an external app project.

  2. It was afterwards that I added the BoolToVisibilityConverter, re-generated the library package, and re-installed it in the app project.

The version number of the package (1.0.0) specified in the .nuspec file was not incremented between the first and second steps, and therefore the new package generated in step 2 was not used by NuGet because a package with the same identifier and version number was already cached at %USERPROFILE%\.nuget\packages\.

This would explain why it worked (refer to the comments on the question) for Sunteen Wu - MSFT (thanks!).

So, solution is to either,

  1. Increment the version number in the .nuspec file, or,

  2. Delete the folder that corresponds to the package (or version thereof), located within the %USERPROFILE%\.nuget\packages folder,

...after which you would re-generate the library package with the same version number, and install it in the app project.

like image 163
ravindUwU Avatar answered Sep 21 '25 01:09

ravindUwU