在 MSBuild 项目中使用 .net core sdk

use .net core sdk in MSBuild project

我有一个 PCL 项目,每个平台都有不同的功能和 类。我现在想实现 .net 核心支持。但是我不能使用像 UserControl 这样的控件,因为没有引用 Microsoft.NET.Sdk.WindowsDesktop SDK。 .net framework很容易实现,因为我只需要引用每个程序集...但是在.net core中,我不能引用程序集...

<Project Sdk="MSBuild.Sdk.Extras">
  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;xamarin.ios10;xamarin.mac20;xamarin.tvos10;monoandroid10.0;tizen40</TargetFrameworks>
    <TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">uap10.0.17763;net472;netcoreapp3.1;$(TargetFrameworks)</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) And '$(OS)' == 'Windows_NT' ">
    ...
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="System.Xaml" />
  </ItemGroup>

  <ItemGroup Condition=" $(TargetFramework.StartsWith('netcoreapp3')) And '$(OS)' == 'Windows_NT' ">
    ...
    <SDKReference Include="Microsoft.NET.Sdk.WindowsDesktop" />
  </ItemGroup>

这是我的可执行应用程序,引用上面的 PCL-项目;

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference ...... />
  </ItemGroup>

</Project>

我已经尝试过引用 SDK,但它不起作用。

 <SDKReference Include="Microsoft.NET.Sdk.WindowsDesktop" />

I want to implement .net core support now. But I cant use controls like UserControl because the Microsoft.NET.Sdk.WindowsDesktop SDK isn't referenced. The .net framework is easy to implement because I only have to reference each assembly... But in .net core, I can't reference the assembly..

深入研究后发现Microsoft.NET.Sdk.WindowsDesktop不能被SDKReference使用。

作为建议,您可以创建一个自定义目标文件,然后将其导入您的 PCL-project 以使用 Net Core SDK。

1) 在您的 PCL 项目文件夹中创建一个名为 custom.targets 的文件。

2) 然后在 custom.targets 中添加这些:

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

    <PropertyGroup>         
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <UseWPF>true</UseWPF>
    </PropertyGroup>

</Project>

3) 将其导入 PCL-Project.

xxx.csproj 文件中
<Import  Project="$(ProjectDir)custom.targets" Condition=" $(TargetFramework.StartsWith('netcoreapp3')) And '$(OS)' == 'Windows_NT' "/>

4) 然后重启你的项目。虽然有些warning是提醒你有些sdks被重复引用了,但是你可以无视,不会对你的项目有任何影响。

你可以看看这个,我这边效果很好。