【发布时间】:2018-09-20 09:13:18
【问题描述】:
我有一个使用 .NET Core 格式定义的 .csproj 项目。该项目包含由 .xaml 文件和关联的 .cs 文件组成的用户控制文件。以前我在这些用户控件上遇到编译错误,直到我将以下内容添加到 .csproj 文件中:
<ItemGroup>
<Page Include="**\*.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</Page>
<Compile Update="**\*.xaml.cs" SubType="Code" DependentUpon="%(Filename)" />
</ItemGroup>
此时解决方案开始在 Visual Studio 中编译。但是,当我尝试使用 dotnet build 从命令行构建时,构建失败并出现我之前在 Visual Studio 中看到的错误:The name 'InitializeComponent' does not exist in the current context.
任何想法为什么解决方案在 VS2017 中构建但不使用 dotnet 构建,或者我如何解决这个问题?
编辑:没有 PackageReferences 的 .csproj 内容
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LanguageTargets>$(MSBuildToolsPath)\Microsoft.CSharp.targets</LanguageTargets>
<TargetFramework>net45</TargetFramework>
<IsPackable>true</IsPackable>
</PropertyGroup>
<ItemGroup>
<Page Include="**\*.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</Page>
<Compile Update="**\*.xaml.cs" SubType="Code" DependentUpon="%(Filename)" />
</ItemGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
</ItemGroup>
</Project>
【问题讨论】:
-
这个项目完全不适合.NETCore,用dotnet.exe构建没有意义。使用 msbuild。
-
@HansPassant 我尝试这样做的原因是因为我正在尝试设计可以在不同解决方案中重复使用的构建内容;我宁愿不解析 .csproj 来确定要使用的构建工具。当你说它不合适时,这是否意味着它完全不兼容?另外,我认为 dotnet build 是在后台运行 MSBuild - 不是这样吗?
-
@ket,这个问题有什么更新吗?你解决了这个问题吗?如果没有,请告诉我有关此问题的最新信息吗?
-
@Leo Liu-MSFT,对不起,我忽略了接受你的回答。感谢您的澄清。
标签: c# wpf visual-studio build msbuild