How to solve the error "Must use PackageReference"?

C#WinformsVisual Studio-2017Windows Runtime

C# Problem Overview


After adding WindowsRuntime to a WinForms-project, building the project fails with Error "Must use PackageReference". The build log is more precise:

>C:[...]\Visual Studio 2017\Projects\Projectname\packages\Microsoft.Windows.SDK.Contracts.10.0.18362.2005\build\Microsoft.Windows.SDK.Contracts.targets(4,5): error : Must use PackageReference

Here's what I did:

Install-Package Microsoft.Windows.SDK.Contracts

with references to:

  • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll,
  • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll
  • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Threading.Tasks.dll
  • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.IO.dll
  • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.InteropServices.WindowsRuntime.dll.

Is it maybe a thing with the .NET version? In the project's packages.config there is 4.6 while the project targets 4.5.

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Windows.SDK.Contracts" version="10.0.18362.2005" targetFramework="net45"/>
<package id="System.Runtime.WindowsRuntime" version="4.6.0" targetFramework="net45" />
<package id="System.Runtime.WindowsRuntime.UI.Xaml" version="4.6.0" targetFramework="net45" />
</packages>

There are no compile errors... only this strange "PackageReference" error.

C# Solutions


Solution 1 - C#

VS2017/VS2019 -> Solution Explorer -> right-click References -> "Migrate packages.config to PackageReference"

VS2019 -> Solution Explorer -> right-click packages.config -> "Migrate packages.config to PackageReference"

I think this happens when you add a .NET core library to a .NET standard project.

Solution 2 - C#

Solution that worked for me was to:

  1. Run a Clean on the project in question
  2. Close Visual Studio
  3. Navigate to the project folder on disk and delete the obj folder
  4. Reopen Visual Studio and Build

Solution 3 - C#

Could be becuse of nugetpackage, for WPF use WPF one.

Solution 4 - C#

Go to your .csproj and remove the references to the package. On my case they were at the end of the XML file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoeView Question on Stackoverflow
Solution 1 - C#patrickView Answer on Stackoverflow
Solution 2 - C#ChrisView Answer on Stackoverflow
Solution 3 - C#TaranView Answer on Stackoverflow
Solution 4 - C#LugubeView Answer on Stackoverflow