简体   繁体   English

如何在Visual Studio 2017中将PCL更改为.net平台标准库?

[英]How can I change a PCL into a .net Platform Standard Library in Visual Studio 2017?

I am trying to figure out how to change a portable .net class library into a .net platform standard library. 我试图弄清楚如何将可移植的.net类库更改为.net平台标准库。 There is a clickable link in the project settings that looks right it says "Target .net platform standard". 在项目设置中有一个可单击的链接,看起来很正确,显示为“ Target .net平台标准”。

目标.net平台

The error which seems absurd to me is: 在我看来荒谬的错误是:

The project's targets cannot be changed. 项目的目标无法更改。 The selected targets require the project to opt-into NuGet 3.0 support, however, Visual Studio cannot automatically do this for you. 选定的目标要求项目选择加入NuGet 3.0支持,但是Visual Studio无法自动为您执行此操作。 Please uninstall all NuGet packages and try again. 请卸载所有NuGet软件包,然后重试。

Is there any way to do this that makes more sense than the absurd approach above of literally deleting all my references and then trying to add them back? 有什么方法可以比上述荒谬的方法更有意义,即从字面上删除我的所有引用,然后尝试重新添加它们? I don't know how to take this project which I didn't write and convert it to to .net standard other than write all the references down on a piece of paper and then try to add them back again. 除了将所有引用写在纸上然后尝试再次添加它们之外,我不知道该如何接受我没有编写的项目并将其转换为.net标准。 Is that literally what I'm being told to do? 这真的是被告知我要做的吗? Is there another way? 还有另一种方法吗? Am I doing this wrong? 我做错了吗?

Secondly, even when I remove all references that can be removed, the project still won't convert. 其次,即使我删除了所有可以删除的引用,该项目仍然无法转换。 I removed Newtonsoft and all other references that can be removed, leaving only .net, system, and system.core, which are not removable. 我删除了Newtonsoft和所有其他可以删除的引用,只留下了不可移动的.net,system和system.core。

I still get the same absurd error. 我仍然得到同样荒谬的错误。 The project I'm trying to modify is an open source project available on github, but I'm purposely trying to make this question more general so it could be useful to others who run into this "Target .net platform standard doesn't work at all" issue. 我要修改的项目是github上可用的开放源代码项目,但是我有意使这个问题更笼统,因此对遇到此“ Target .net平台标准不起作用的其他人可能有用”完全没有”的问题。

Same here 同样在这里

I struggled a lot with the exact same problem: The solution which worked best was the one provided by @Jasper H Bojsen 我为一个完全相同的问题付出了很多努力:最有效的解决方案是@Jasper H Bojsen提供的解决方案

So I had to: 所以我必须:

  • create a new project regarding the namespaces I was using in the pcl 创建一个关于我在pcl中使用的名称空间的新项目
  • ReNuggetManageDownloadPackagesReferences ReNuggetManageDownloadPackages参考
  • and poof it compiled and I had no error 并编译它,我没有错误

You may need to modify the project file by hand. 您可能需要手动修改项目文件。 It takes a bit of work, but here's some things which can help. 这需要一些工作,但是有些事情会有所帮助。

Before you start, backup your source if it is not hosted somewhere else. 在开始之前,如果源未托管在其他地方,请对其进行备份。

  • First, make sure the project is using the same file format for the project file as you're about to create in the next step. 首先,请确保该项目使用的项目文件格式与下一步中要创建的文件格式相同。

  • Next, create a new solution and project which targets the framework you're looking for, and add the NuGet packages you're using. 接下来,创建一个针对您正在寻找的框架的新解决方案和项目,并添加您正在使用的NuGet软件包。

  • Close the new solution and open the one you're looking to change, and after it's loaded, unload the project in question. 关闭新解决方案,然后打开要更改的解决方案,然后在加载后,卸载有问题的项目。 Edit the project file, and using either a diff tool or by hand open your new project file you created and compare the changes between the two project files. 编辑项目文件,然后使用差异工具或手动打开您创建的新项目文件,并比较两个项目文件之间的更改。 There are several key sections which you want to pay attention to. 您需要注意几个关键部分。

  • Once you've finished editing the project file, close it and reload the project. 完成项目文件的编辑后,将其关闭并重新加载项目。 Note any errors which may be returned, and correct as needed. 注意可能返回的任何错误,并根据需要进行纠正。 If everything works correctly, try compiling. 如果一切正常,请尝试编译。

This is just one possible solution, but from experience there's lots of little gotchas with project files, so don't take this as a definitive solution. 这只是一种可能的解决方案,但是从经验来看,项目文件有很多小问题,所以请不要将其作为最终解决方案。

Below is an example of the difference between the two different project files, so moving from PCL -> Standard will be much easier than the reverse. 下面是两个不同项目文件之间差异的示例,因此从PCL-> Standard迁移比反向迁移要容易得多。 (Note: These are just basic projects which I created quickly, and may not be representative of what you find.) (注意:这些只是我快速创建的基本项目,可能并不代表您找到的内容。)

PCL 聚氯乙烯

<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>
        <MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{66c3d80b-6265-468a-82e1-b76317f9a9a5}</ProjectGuid>
        <OutputType>Library</OutputType>
        <RootNamespace>PCLTest</RootNamespace>
        <AssemblyName>PCLTest</AssemblyName>
        <DefaultLanguage>en-US</DefaultLanguage>
        <FileAlignment>512</FileAlignment>
        <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
        <TargetFrameworkProfile></TargetFrameworkProfile>
        <TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <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' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <ItemGroup>
        <None Include="project.json" />
        <!-- A reference to the entire .NET Framework is automatically included -->
      </ItemGroup>
      <ItemGroup>
        <Compile Include="Class1.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
      </ItemGroup>
      <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
    </Project>

Standard 标准

<Project Sdk="Microsoft.NET.Sdk">

      <PropertyGroup>
        <TargetFramework>netstandard1.4</TargetFramework>
      </PropertyGroup>

    </Project>

I know that this is probably not the answer you are hoping to get and I can't rule out that others have found different routes to success. 我知道这可能不是您希望得到的答案,并且我不能排除其他人找到了成功的不同途径。

Anyhow. 无论如何。 I had 6-7 PCL's that I wanted to move to .NET Standard. 我有6-7个PCL ,我想转到.NET Standard。 After fiddling around with it for a while trying to find the automagical way forward, I finally landed on a much more pragmatic approach. 在尝试了一段时间之后,我尝试了一下自动方法,最终我找到了一种更加实用的方法。

What I did was to create a all new .NET Standard project with Visual Studio 2017 (v15.2 or later). 我要做的是使用Visual Studio 2017(v15.2或更高版本)创建一个全新的.NET Standard项目。 Having the PCL project and the new .NET Standard project opened side-by-side (two monitors help here) I basically recreated the folder structure in the new project (using Visual Studio) copied over the files (using Explorer), added the files to the new project manually (using Visual Studio) as well as included and reestablished the references manually. 并排打开PCL项目和新的.NET Standard项目(这里有两个监视器帮助),我基本上是在新项目(使用Visual Studio)中重新创建了复制到文件(使用资源管理器)的文件夹结构,并添加了文件(使用Visual Studio)手动添加到新项目,以及手动包含和重新建立引用。

Also, remember to set the Assembly Name and Default Namespace to be identical to the PCL Assembly Name and Default Namespace . 另外,请记住将程序集名称默认名称空间设置为与PCL 程序集名称默认名称空间相同 You get to these by right-clicking on the project and choosing Properties and the Application tab: 通过右键单击项目,然后选择“属性”和“应用程序”选项卡,可以找到这些内容:

在此处输入图片说明

I know this sounds like boring work, but trust me, it doesn't take very long and the process is remarkably resistant to error. 我知道这听起来像是无聊的工作,但是请相信我,这并不需要很长时间,而且该过程非常容易出错。 When the new project compiles you are done. 新项目编译完成后。 If it doesn't, it's typically easy to figure out what is missing. 如果没有,通常很容易找出缺少的内容。 Also, when it's works, you know that you've started from a clean slate and that you are not dragging over something that will come back and haunt you later. 而且,当它起作用时,您知道您是从一开始就开始的,并且您不会拖延会回来并在以后困扰您的内容。

Note : You might run into a System.* libraries missing in .NET Standard. 注意 :您可能会遇到.NET Standard中缺少的System。*库。 If that happens see if you can add is as a NuGet's. 如果发生这种情况,请查看是否可以将其添加为NuGet。 They typically have a version 4.3.*. 它们通常具有4.3。*版本。 An example of this could be System.Net.Security . 例如: System.Net.Security

It is also possible that you PCL relies on libraries that are not compatible with the version .NET Standard you decided on or any .NET Standard for that matter. 您的PCL也可能依赖与您决定使用的.NET Standard版本或任何.NET Standard版本不兼容的库。 In the first case try and see if you can live with a higher version .NET Standard. 在第一种情况下,请尝试看看是否可以使用更高版本的.NET Standard。 In the latter case you are currently out of luck, but look out for .NET Standard 2.0, which will have a much broader .NET API support. 在后一种情况下,您目前不走运,但是请注意.NET Standard 2.0,它将提供更广泛的.NET API支持。

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

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