简体   繁体   English

Nuget包在Visual Studio中无法正常工作

[英]Nuget Packages not working correctly in Visual Studio

I am writing a Windows Phone app, at the beggining using VS 2010 Express for windows phone. 我正在编写一个Windows Phone应用程序,在使用VS 2010 Express for Windows phone开始。 Then I installed VS 2012 and i made a desktop app which consumed JSON/REST service using RestSharp. 然后我安装了VS 2012,我制作了一个使用RestSharp消耗JSON / REST服务的桌面应用程序。 Due to lack of support for plugins in Express version, i got full VS 2010 Ultimate and installed Nuget. 由于缺乏对Express版本插件的支持,我得到了完整的VS 2010 Ultimate并安装了Nuget。 When I create a win phone library project, and i add a RestSharp package, it shows in References, but i cannot access any of it's classes (and using RestSharp is underlined in red color). 当我创建一个win手机库项目,并添加一个RestSharp软件包时,它显示在References中,但是我无法访问它的任何类(并且使用RestSharp以红色加下划线)。 Also, when i Remove it and add again from Add Reference i get an Incompatible reference error window: 此外,当我删除它并从添加引用再次添加时,我得到一个不兼容的引用错误窗口:

RestSharp.WindowsPhone, Version=103.2.0.0, Culture=neutral, PublicKeyToken=null" is incompatible with 
Windows Phone 7.1
In order to add it yourself you should to change the project's terget to a compatible framework first.

It also appears if i change the target to WP 7.0. 如果我将目标更改为WP 7.0,也会出现。

Has anyone solved similar problem? 有人解决了类似的问题吗?

In my case this was because there was a packages.config file in the project folder but not in the solution . 在我的情况下,这是因为项目文件夹中有一个packages.config文件, 但解决方案中没有 This was why I could not add the reference again properly. 这就是我无法再次正确添加引用的原因。 Delete this file from the physical disk, and re-run your Install-Package command. 从物理磁盘中删除此文件,然后重新运行Install-Package命令。 It should add this correctly now. 它现在应该正确添加。

Are you using source control? 你在使用源代码管理吗?

The it might be possible that you have not yet set the solution to enable package restore (NuGet documentation). 您可能尚未设置解决方案以启用程序包还原 (NuGet文档)。

Right click on the Solution node in Solution Explorer and select Enable NuGet Package Restore. 右键单击Solution Explorer中的Solution节点,然后选择Enable NuGet Package Restore。

After that Solution Explorer will contain a few items more and there will be a new folder packages that was automatically added to your solution folder. 之后,解决方案资源管理器将包含更多项目,并且会有一个新的文件packages自动添加到您的解决方案文件夹中。 You will need this folder to add to your version control because it will contain your installed NuGet packages . 您需要将此文件夹添加到版本控件中,因为它将包含已安装的NuGet包

That's it. 而已。

If you want to know more, here's more details on what it (automatically) does for you: 如果您想了解更多信息,请点击此处(自动)为您提供的更多详细信息:

It added a solution folder named .nuget containing NuGet.exe and a NuGet.targets MsBuild file. 它添加了一个名为.nuget的解决方案文件夹,其中包含NuGet.exe和一个NuGet.targets MsBuild文件。 It also changed every project in the solution to import the NuGet.targets MsBuild task. 它还更改了解决方案中的每个项目以导入NuGet.targets MsBuild任务。

With this in place, any time a project is compiled, the build task will look at each project's packages.config file and for each package in that file, ensure that the corresponding package exists within the packages folder. 有了这个,每次编译项目时,构建任务都会查看每个项目的packages.config文件,并且对于该文件中的每个包,确保packages文件夹中存在相应的包。 For any missing package, the build task will download and unpack the package. 对于任何缺少的包,构建任务将下载并解压缩包。

In the restore scenario, NuGet will grab the exact version when restoring a package. 在还原方案中,NuGet将在还原包时获取确切的版本。 It will not perform any upgrades. 它不会执行任何升级。

Additionally, if you have the latest NuGet version installed, will now find a new option unter Tools -> Options... -> Package Manager -> General -> Allow NuGet to download missing packages during build that I would also suggest to use. 此外,如果您安装了最新的NuGet版本,现在将找到一个新选项unter 工具 - >选项... - >包管理器 - >常规 - > 允许NuGet在构建期间下载丢失的包 ,我也建议使用。

The name says it all. 这个名字说明了一切。 If the solution is configured to use a certain NuGet package but the package is not yet installed on your development machine, NuGet will download it automatically for you when you do your next build . 如果解决方案配置为使用某个NuGet包但开发计算机上尚未安装该软件包,则NuGet会在您进行下一次build时自动为您下载该软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM