首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据选定的配置将项目构建为可移植的或普通的类库?

如何根据选定的配置将项目构建为可移植的或普通的类库?
EN

Stack Overflow用户
提问于 2016-02-10 19:22:19
回答 2查看 1.9K关注 0票数 2

上下文:我们有一个名为“该项目”的公共库项目,它与Xamarin移动解决方案和visual中的一个web项目共享。由于Xamarin中的限制,这个共享库是一个PCL Profile259。在我们的web应用程序解决方案中,我们希望用适当的属性标记数据模型,使DataContext能够正确工作(因为我们希望使用LinqToSql)。

尝试解决方案:尝试解决方案:我们决定查看“项目”的项目,并以一种方式更改msbuild,使其能够存在于可移植库/普通类库的状态中,这取决于visual中所选的配置。

我们已经在这个职位中看到了解决方案,但是它只是解决方案的一部分。我们可以让项目作为一个或另一个存在,但是它需要对项目文件进行手动更改。现在,我们得到了加载以下项目的错误。可移植项目的名称必须是“..NETPortable”。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">DebugDesktop</Configuration>
        <Platform Condition="'$(Platform)' == ''">AnyCPU</Platform>
        <ProjectGuid>{426E7BD8-9DA8-4E15-9512-72E7C632B037}</ProjectGuid>
        <OutputType>Library</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>THEPROJECT</RootNamespace>
        <AssemblyName>THEPROJECT</AssemblyName>
        <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
        <MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
        <BuildMode>Desktop</BuildMode>
    </PropertyGroup>

    <!-- Define the Build Configurations -->
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugDesktop|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Desktop\Debug\</OutputPath>
        <DefineConstants>TRACE;DEBUG;</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <ConsolePause>false</ConsolePause>
        <BuildMode>Desktop</BuildMode>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseDesktop|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Desktop\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <BuildMode>Desktop</BuildMode>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugPCL|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\PCL\Debug\</OutputPath>
        <DefineConstants>TRACE;DEBUG;</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <ConsolePause>false</ConsolePause>
        <BuildMode>Portable</BuildMode>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleasePCL|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\PCL\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <BuildMode>Portable</BuildMode>
    </PropertyGroup>

    <!-- Determine the correct properties for PCL/Desktop -->
    <Choose>
        <When Condition=" '$(BuildMode)' == 'Portable' ">
            <PropertyGroup>
                <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
                <TargetFrameworkProfile>Profile259</TargetFrameworkProfile>
                <TargetFrameworkIdentifier>.NETPortable</TargetFrameworkIdentifier>
                <DynamicImportPath>$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets</DynamicImportPath>
            </PropertyGroup>
        </When>
        <When Condition=" '$(BuildMode)' == 'Desktop' ">
            <PropertyGroup>
                <FileAlignment>512</FileAlignment>
                <TargetFrameworkProfile />
                <DynamicImportPath>$(MSBuildToolsPath)\Microsoft.CSharp.targets</DynamicImportPath>
            </PropertyGroup>
            <ItemGroup>
                <Reference Include="System.Data.Linq" />
            </ItemGroup>
        </When>
    </Choose>

    <ItemGroup>
        <Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
            <HintPath>..\..\mobileapplication\packages\Newtonsoft.Json.8.0.2\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
            <Private>True</Private>
        </Reference>
        <Reference Include="System" />
        <Reference Include="System.Core" />
        <Reference Include="System.Xml.Linq" />
        <Reference Include="Microsoft.CSharp" />
        <Reference Include="System.Xml" />
    </ItemGroup>
    <ItemGroup>
        <ProjectReference Include="..\..\sqlite-net-extensions\SQLiteNetExtensions\SQLiteNetExtensions-PCL.csproj">
            <Project>{f723017d-ede5-49cc-a84f-881c067c6004}</Project>
            <Name>SQLiteNetExtensions-PCL</Name>
        </ProjectReference>
        <ProjectReference Include="..\..\sqlitenetpcl\src\SQLite.Net\SQLite.Net.csproj">
            <Project>{4971d437-0694-4297-a8cc-146ce08c3bd9}</Project>
            <Name>SQLite.Net</Name>
        </ProjectReference>
    </ItemGroup>

    <Import Project="$(DynamicImportPath)" />

    <!-- Source/Resx etc... -->
</Project>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-09 16:15:42

当您创建一个可移植类库时,您需要将您的库创建为Windows8.1可移植库,您不能为此使用通用库,这听起来就像受Xamarin约束所做的那样。Windows8.1便携式库是一种混合库,它允许经典的.net与新的轻量级.net版本进行通信。

便携式类库的问题是它不能通过项目引用引用一个经典类库,而只能通过DLL引用引用它。如果不愿意编辑该文件,只需找到已编译的DLL并手动添加引用,就不需要处理XML。如果您选择这样做,则需要手动更新构建依赖项,否则可能会中断引用。

问题似乎不完全在您的.csproj中,但也在您的project.json中。我注意到visual没有生成正确的project.json文件。我必须修改UI中的目标以使其正确生成。一旦获得要生成的初始project.json,就没有其他问题了。

在做任何事情之前,我们需要连接我们的便携式类库来处理一个经典类库。第一步是将目标框架从属性更新为目标.net 4.5或任何您喜欢的版本。

完成此操作后,请确认project.json文件如下所示:

代码语言:javascript
复制
{
  "supports": {},
  "dependencies": {},
  "frameworks": {
    ".NETPortable,Version=v4.5,Profile=Profile259": {}
  }
}

一旦可移植类库的目标是正确的版本,您就可以向编译的.net 4.5程序集添加一个DLL引用。我没有对可移植类库的csproj文件进行任何额外的更改,但是这里是源代码,因此您可以查看一下。

请注意如何引用ClassLibrary1:

代码语言:javascript
复制
    <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{7C4EB968-4C17-438C-B981-97EDC865F312}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>PortableLibrary</RootNamespace>
    <AssemblyName>PortableLibrary</AssemblyName>
    <DefaultLanguage>en-US</DefaultLanguage>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <TargetFrameworkProfile>Profile259</TargetFrameworkProfile>
    <TargetFrameworkVersion>v4.5</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>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="ClassLibrary1">
      <HintPath>..\ClassLibrary1\bin\Debug\ClassLibrary1.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <None Include="project.json" />
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

这将消除假设您的web项目是ASP.NET Core1.0的问题。如果您正在运行一个经典ASP.NET应用程序,您可能仍然需要创建构建规则。

TLDR:

看起来您的project.json文件有问题,请确保它是正确生成的,并且您不能通过项目引用直接引用.net 4.5,您必须引用DLL。

票数 1
EN

Stack Overflow用户

发布于 2017-06-23 00:41:57

要回答关于使项目成为“可移植的”或“正常的”(取决于配置)的问题,如果弹出的错误是关于<TargetFrameworkIdentifier>的,则必须是.NETPortable

<ProjectTypeGuids> 从配置.中删除所有

那些项目类型的GUI为Visual提供了一些只与可移植项目相关的GUI,因此当配置不可移植时,它会导致您得到的错误。但是,由于您可以轻松地编辑XML,所以显然不需要这些内容。此外,您还可以继续使用GUI编辑项目属性,并删除项目类型GUID。

至少在2015中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35324118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档