无法在框架 4.7.2 中恢复 nuget 包

Cant restore nuget packages in framework 4.7.2

我有一个 WPF 项目。它具有三个用 .Net Framework 4.7.2 编写的库。我正在尝试为此解决方案恢复 nuget 包,但我做不到。

当我右键单击 sln 文件,然后选择 Restore Nuget Packages 似乎一切正常:

但是在我 运行 程序之后,它仍然缺少所有引用:

在每个 csproj packagaes.config 文件中存在。例如:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Autofac" version="5.0.0" targetFramework="net472" />
  <package id="Caliburn.Micro" version="4.0.62-alpha" targetFramework="net472" />
  <package id="Caliburn.Micro.Core" version="4.0.62-alpha" targetFramework="net472" />
  <package id="LiveCharts" version="0.9.7" targetFramework="net472" />
  <package id="LiveCharts.Wpf" version="0.9.7" targetFramework="net472" />
  <package id="MaterialDesignColors" version="1.2.0" targetFramework="net472" />
  <package id="MaterialDesignExtensions" version="2.7.0" targetFramework="net472" />
  <package id="MaterialDesignThemes" version="2.6.0" targetFramework="net472" />
  <package id="Microsoft.Bcl.AsyncInterfaces" version="1.1.0" targetFramework="net472" />
  <package id="Microsoft.Xaml.Behaviors.Wpf" version="1.0.1" targetFramework="net472" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" />
  <package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net472" />
</packages>

我的问题是,如何从这个项目中恢复所有的 nuget 包?我正在 Visual Studio 2019 年工作。

编辑:

我的一个 .csproj:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{07453180-4038-457C-81BB-7017A2890660}</ProjectGuid>
    <OutputType>WinExe</OutputType>
    <RootNamespace>Thinning.UI</RootNamespace>
    <AssemblyName>Thinning.UI</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <WarningLevel>4</WarningLevel>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Autofac, Version=4.9.4.0, Culture=neutral, PublicKeyToken=17863af14b0044da" />
    <Reference Include="Caliburn.Micro.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=8e5891231f2ed21f, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Caliburn.Micro.Core.4.0.62-alpha\lib\net45\Caliburn.Micro.Core.dll</HintPath>
    </Reference>
    <Reference Include="Caliburn.Micro.Platform, Version=4.0.0.0, Culture=neutral, PublicKeyToken=8e5891231f2ed21f, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Caliburn.Micro.4.0.62-alpha\lib\net45\Caliburn.Micro.Platform.dll</HintPath>
    </Reference>
    <Reference Include="LiveCharts, Version=0.9.7.0, Culture=neutral, PublicKeyToken=0bc1f845d1ebb8df, processorArchitecture=MSIL">
      <HintPath>..\..\packages\LiveCharts.0.9.7\lib\net45\LiveCharts.dll</HintPath>
    </Reference>
    <Reference Include="LiveCharts.Wpf, Version=0.9.7.0, Culture=neutral, PublicKeyToken=0bc1f845d1ebb8df, processorArchitecture=MSIL">
      <HintPath>..\..\packages\LiveCharts.Wpf.0.9.7\lib\net45\LiveCharts.Wpf.dll</HintPath>
    </Reference>
    <Reference Include="MaterialDesignColors, Version=1.2.0.325, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\..\packages\MaterialDesignColors.1.2.0\lib\net45\MaterialDesignColors.dll</HintPath>
    </Reference>
    <Reference Include="MaterialDesignExtensions, Version=2.7.0.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\..\packages\MaterialDesignExtensions.2.7.0\lib\net45\MaterialDesignExtensions.dll</HintPath>
    </Reference>
    <Reference Include="MaterialDesignThemes.Wpf, Version=2.6.0.325, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\..\packages\MaterialDesignThemes.2.6.0\lib\net45\MaterialDesignThemes.Wpf.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Xaml.Behaviors, Version=1.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\..\packages\Microsoft.Xaml.Behaviors.Wpf.1.0.1\lib\net45\Microsoft.Xaml.Behaviors.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Drawing.Design" />
    <Reference Include="System.Xml" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xaml">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>
  <ItemGroup>
    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>
    <Compile Include="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="Helpers\AlgorithmTest.cs" />
    <Compile Include="Helpers\CardContent.cs" />
    <Compile Include="Helpers\Extensions\SimpleContainerExtension.cs" />
    <Compile Include="Helpers\FileDialog.cs" />
    <Compile Include="Helpers\Hardware.cs" />
    <Compile Include="Helpers\ImageLabelModel.cs" />
    <Compile Include="Helpers\Interfaces\IAlgorithmTest.cs" />
    <Compile Include="Helpers\Interfaces\ICardContent.cs" />
    <Compile Include="Helpers\Interfaces\IFileDialog.cs" />
    <Compile Include="Helpers\Interfaces\IHardware.cs" />
    <Compile Include="Helpers\Interfaces\IMainWindowViewModelHelper.cs" />
    <Compile Include="Helpers\Interfaces\IPerformanceChartViewModelHelper.cs" />
    <Compile Include="Helpers\Interfaces\IViewModel.cs" />
    <Compile Include="Helpers\MainWindowViewModelHelper.cs" />
    <Compile Include="Helpers\PerformanceChartViewModelHelper.cs" />
    <Compile Include="ViewModels\MainWindowViewModel.cs" />
    <Compile Include="ViewModels\PerformanceChartViewModel.cs" />
    <Compile Include="ViewModels\ProgressViewModel.cs" />
    <Compile Include="Views\ViewBoxTracking.cs" />
    <Compile Include="Views\MainWindowView.xaml.cs">
      <DependentUpon>MainWindowView.xaml</DependentUpon>
    </Compile>
    <Compile Include="Views\PerformanceChartView.xaml.cs">
      <DependentUpon>PerformanceChartView.xaml</DependentUpon>
    </Compile>
    <Compile Include="Views\ProgressView.xaml.cs">
      <DependentUpon>ProgressView.xaml</DependentUpon>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Bootstrapper.cs" />
    <Compile Include="Properties\AssemblyInfo.cs">
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="Properties\Resources.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
    <Compile Include="Properties\Settings.Designer.cs">
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    </Compile>
    <EmbeddedResource Include="Properties\Resources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
    <AdditionalFiles Include="..\..\Settings.stylecop">
      <Link>Settings.stylecop</Link>
    </AdditionalFiles>
    <None Include="packages.config" />
    <None Include="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Thinning.Algorithm\Thinning.Algorithm.csproj">
      <Project>{9bfb384e-ca70-407a-9e0e-b9ac235ed7d6}</Project>
      <Name>Thinning.Algorithm</Name>
    </ProjectReference>
    <ProjectReference Include="..\Thinning.Infrastructure\Thinning.Infrastructure.csproj">
      <Project>{19B5AECE-CCE3-4312-AB19-D190AF1A1441}</Project>
      <Name>Thinning.Infrastructure</Name>
    </ProjectReference>
  </ItemGroup>
  <ItemGroup>
    <Page Include="Views\MainWindowView.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:Compile</Generator>
    </Page>
    <Page Include="Views\PerformanceChartView.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:Compile</Generator>
    </Page>
    <Page Include="Views\ProgressView.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:Compile</Generator>
    </Page>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

有两种管理 nuget 包的方法:Packages.config and PackageReference.

根据你的图片我们可以看到你在你的项目中同时使用了这两种格式,这是导致未知恢复问题的原因。

(黄色的表示Packages.config,蓝色的表示PackageReference格式)

我们不应在一个项目中同时使用两种格式,因此解决您问题的方法是强制您的项目仅使用一种格式。由于您的三个项目都使用 packages.config,您可以考虑删除项目文件中的 PackageReference 定义。

解决方法:

编辑您的项目文件并删除 <PackageReference Include="StyleCop.Analyzers">xxx</PackageReference><RestoreProjectStyle>PackageReference</RestoreProjectStyle> 等定义(如果存在)。