通过NuGet包访问的程序集不包括类

时间:2022-08-10 15:17:37

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

我创建的通用类库(UWP)项目有两个类;

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

1.公共BoolToVisibilityConverter - 一个IValueConverter,它负责将bool转换为XAML可见性,反之亦然。

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

2.一个公共CustomControl模板控件,其模板(在Themes \ Generic.xaml(下面的项目结构)中定义)使用上面提到的BoolToVisibilityConverter。

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).

值,Visibility绑定的属性,是控件上的布尔依赖属性,local是BoolToVisibilityConverter所在的命名空间的别名(与模板化控件的属性相同)。

Project Structure

通过NuGet包访问的程序集不包括类

Build Configurations

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

项目的发布构建配置启用了“XML文档文件”和“生成库布局”选项。

通过NuGet包访问的程序集不包括类

Generating a NuGet package

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

我的项目的NuSpec(位于NuGet \ RefClassLibrary.nuspec)是;

<?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.

该文件是使用nuget.exe spec命令生成的,NuGet CLI版本为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,

为了生成NuGet包,我首先删除了bin和obj文件夹,并在Release(Any CPU)配置中构建了项目,该配置在bin文件夹中生成了以下结构,

通过NuGet包访问的程序集不包括类

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,

然后,我使用NuGet \文件夹中的命令nuget.exe pack RefClassLibrary.nuspec为类库生成NuGet包,该文件生成了具有以下结构的nupkg文件,

通过NuGet包访问的程序集不包括类

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.

在新的通用Windows应用程序项目中,我在安装生成的程序包的Visual Studio中的程序包管理器控制台中运行了安装程序包命令,其中包含nupkg的绝对路径作为其参数。

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

在构建项目之后(没有错误),我将以下XAML添加到新的空白页面,

<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

通过NuGet包访问的程序集不包括类

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

在对象浏览器中查看RefClassLibrary时,确实如错误所示,BoolToVisibilityConverter类不存在......

通过NuGet包访问的程序集不包括类

Consuming the class library directly

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

我使用Uninstall-Package RefClassLibrary命令从项目中删除包。

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.

然后我使用命令7z x RefClassLibrary.1.0.0.nupkg -oOut解压缩nupkg文件,将其解压缩到文件夹Out,并直接引用项目中Out \ lib \ uap10.0 \文件夹中包含的dll。通过解决方案资源管理器添加参考,使其完美运行。 BoolToVisibilityConverter确实显示在对象浏览器中。

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...

正确的程序集包含在NuGet包中,尽管它的某些部分(?)没有被“导入”到项目中......

1 个解决方案

#1


0  

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

当通过Install-Package Cmdlet安装NuGet包时,据我所知,这是NuGet的作用,

  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.

    检索要安装的包的标识符和版本号。它通过指定的参数(在未指定版本号的情况下,默认为最新的'stable'版本)或通过读取它指向的包的内容来实现。

  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.

    .nu​​pkg文件和与该程序包关联的一些其他资产将复制到%USERPROFILE%\。nuget \ packages \ {identifier} \ {version} \文件夹中并缓存。

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

    然后提取包并将其安装在您所在的Visual Studio项目中。

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.

当您再次安装相同版本的相同软件包时,NuGet将使用位于上述文件夹中的缓存文件,而不是重新下载它们。


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

问题中描述的问题源于我没有递增NuGet包的版本号。

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.

    我最初只使用CustomControl创建了库项目,生成了库包,并将其安装在外部应用程序项目中。

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

    之后我添加了BoolToVisibilityConverter,重新生成了库包,并将其重新安装到app项目中。

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\.

.nu​​spec文件中指定的包(1.0.0)的版本号在第一步和第二步之间没有增加,因此NuGet不使用步骤2中生成的新包,因为具有相同标识符和版本的包数字已经缓存在%USERPROFILE%\。nuget \ packages \。

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

这可以解释为什么它对Sunteen Wu - MSFT有效(请参阅对该问题的评论)(谢谢!)。

So, solution is to either,

所以,解决方案是,

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

    增加.nuspec文件中的版本号,或者,

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

    删除与%USERPROFILE%\。nuget \ packages文件夹中的包(或其版本)对应的文件夹,

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

...之后,您将使用相同的版本号重新生成库包,并将其安装在应用程序项目中。

#1


0  

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

当通过Install-Package Cmdlet安装NuGet包时,据我所知,这是NuGet的作用,

  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.

    检索要安装的包的标识符和版本号。它通过指定的参数(在未指定版本号的情况下,默认为最新的'stable'版本)或通过读取它指向的包的内容来实现。

  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.

    .nu​​pkg文件和与该程序包关联的一些其他资产将复制到%USERPROFILE%\。nuget \ packages \ {identifier} \ {version} \文件夹中并缓存。

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

    然后提取包并将其安装在您所在的Visual Studio项目中。

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.

当您再次安装相同版本的相同软件包时,NuGet将使用位于上述文件夹中的缓存文件,而不是重新下载它们。


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

问题中描述的问题源于我没有递增NuGet包的版本号。

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.

    我最初只使用CustomControl创建了库项目,生成了库包,并将其安装在外部应用程序项目中。

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

    之后我添加了BoolToVisibilityConverter,重新生成了库包,并将其重新安装到app项目中。

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\.

.nu​​spec文件中指定的包(1.0.0)的版本号在第一步和第二步之间没有增加,因此NuGet不使用步骤2中生成的新包,因为具有相同标识符和版本的包数字已经缓存在%USERPROFILE%\。nuget \ packages \。

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

这可以解释为什么它对Sunteen Wu - MSFT有效(请参阅对该问题的评论)(谢谢!)。

So, solution is to either,

所以,解决方案是,

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

    增加.nuspec文件中的版本号,或者,

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

    删除与%USERPROFILE%\。nuget \ packages文件夹中的包(或其版本)对应的文件夹,

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

...之后,您将使用相同的版本号重新生成库包,并将其安装在应用程序项目中。