WPF应用程序在vs2k8中生成,但不在命令行w/msbuild上

WPF应用程序在vs2k8中生成,但不在命令行w/msbuild上,wpf,.net-3.5,msbuild,msbuild-task,Wpf,.net 3.5,Msbuild,Msbuild Task,我有一个相当小的解决方案,包括一个WPF windows应用程序。从解决方案构建时,它构建得非常好。我最近将解决方案中包含的项目集成到一个使用MSBuild的更大的命令行生成中。但是,从命令行构建时,我会出现以下错误: MainWindow.xaml.cs(12,4): error CS0103: The name 'InitializeComponent' does not exist in the current context View\EngineMonitorView.xaml.cs(

我有一个相当小的解决方案,包括一个WPF windows应用程序。从解决方案构建时,它构建得非常好。我最近将解决方案中包含的项目集成到一个使用MSBuild的更大的命令行生成中。但是,从命令行构建时,我会出现以下错误:

MainWindow.xaml.cs(12,4): error CS0103: The name 'InitializeComponent' does not exist in the current context
View\EngineMonitorView.xaml.cs(12,4): error CS0103: The name 'InitializeComponent' does not exist in the current context
View\HostingEngineView.xaml.cs(12,4): error CS0103: The name 'InitializeComponent' does not exist in the current context
View\MainView.xaml.cs(13,4): error CS0103: The name 'InitializeComponent' does not exist in the current context
View\MainView.xaml.cs(17,52): error CS0103: The name 'gView' does not exist in the current context
View\PerformanceCounterView.xaml.cs(22,4): error CS0103: The name 'InitializeComponent' does not exist in the current context
View\PerformanceCounterView.xaml.cs(117,22): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(118,20): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(127,4): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(138,5): error CS0103: The name 'txtScale' does not exist in the current context
View\PerformanceCounterView.xaml.cs(179,5): error CS0103: The name 'txtLast' does not exist in the current context
View\PerformanceCounterView.xaml.cs(180,5): error CS0103: The name 'txtMin' does not exist in the current context
View\PerformanceCounterView.xaml.cs(181,5): error CS0103: The name 'txtMax' does not exist in the current context
View\PerformanceCounterView.xaml.cs(189,5): error CS0103: The name 'txtAverage' does not exist in the current context
View\PerformanceCounterView.xaml.cs(250,4): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(251,4): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(253,27): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(255,27): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(264,4): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(269,4): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(274,4): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(279,4): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(293,5): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(303,5): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(318,5): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(325,5): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\PerformanceCounterView.xaml.cs(342,4): error CS0103: The name 'cnvsCounterGrid' does not exist in the current context
View\ServerMonitorView.xaml.cs(12,4): error CS0103: The name 'InitializeComponent' does not exist in the current context
View\ServerTreeView.xaml.cs(12,4): error CS0103: The name 'InitializeComponent' does not exist in the current context
View\ServiceDetailView.xaml.cs(12,4): error CS0103: The name 'InitializeComponent' does not exist in the current context
View\ServiceMonitorView.xaml.cs(12,4): error CS0103: The name 'InitializeComponent' does not exist in the current context
我在根MSBuild.proj文件中包含了.NET 3.5中的WinFX目标文件,因为它似乎没有包含在其他任何地方:

<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />

不过,这似乎没有影响任何事情,我仍然会遇到错误。据我所知,将.xaml文件预编译为.cs文件、嵌入和连接资源等的自定义WPF构建任务似乎没有运行,这就是为什么找不到InitializeComponent和我的视图中定义的任何控件的原因。但是,我对为什么感到茫然……而试图浏览与WPF和MSBuild相关的无数搜索结果对我来说毫无意义

更新:


将Microsoft.WinFX.targets添加到.csproj文件似乎可以。但是,这样做会导致项目无法在VisualStudio2008中构建。不知何故,VS包含了你的目标…但我不确定如何。有人更了解VS如何构建WPF项目吗?是否有一个主构建文件隐藏在导入适当目标的地方

查看MSDN SDK调用的这一部分,确保您正在执行所需的所有操作。

如果不直接查看项目文件,很难确定,但您的项目似乎缺少了
发送msbuild解决方案文件而不是.csproj文件,有时可能会有所帮助:

msbuild.exe yoursolution.sln
此外,devenv.exe本身提供了一个命令行构建,应该与IDE中的体验相当:

devenv.exe /build yoursolution.sln

我意识到编辑已经回答了这个问题,但我决定在这里包含更多信息和答案,并为您最近编辑的Visual Studio相关问题提供建议

编辑csproj文件,并在末尾附近放置一行类似于此的内容,靠近包含“Microsoft.CSharp.targets”的行(可以改为MSBuildToolsPath):


看,还有

目标决定了项目的实施方式 实际建造,并取决于两者 属性和项目。WPF 应用程序必须同时具有 特定语言的目标和目标 WPF特定目标:

目标是结束的独立文件 使用.targets扩展名。这个 随附的目标文件 .NET Framework 3.0安装在 以下地点:

%WINDIR%\Microsoft.NET\Framework\vX.X.X

特定于语言的目标包含 构建特定语言的逻辑 源代码。特定语言 C#的目标是 Microsoft.CSharp.targets,以及 Microsoft.VisualBasic.targets for Visual Basic。这两个目标 派生并扩展 Microsoft.Common.targets目标,其中 执行大部分公共事务, 独立于语言的构建工作

至于“为什么VisualStudio可以构建这个”——我猜测VisualStudio在幕后包含了这个。您可以通过临时重命名C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.WinFX.target并查看visual studio是否可以继续构建来向自己证明这一点。

如果您想问,visual studio是否可以工作,为什么不使用msbuild? 那么答案很可能是您使用了错误的msbuild版本。 您将需要msbuild 4来生成.net 4项目


另请参见以下链接

在我发布问题之前,我确实读过那篇文章。这就是我了解Microsoft.WinFX.targets的原因。但是,使用msbuild在命令行中生成仍然不起作用。非常令人困惑的问题。嗯,我的意思是它显然没有正确处理XAML。它应该是找到你的物品并处理它们。这些是MSBuild中唯一的错误/警告吗?可能会在调用MSBuild时抛出/verbosity:detailed开关,以查看场景下是否有某些东西在默默地失败。是的,这些是唯一的错误。没有其他警告…该项目没有任何警告。我很乐意在这方面为您提供更多帮助,但我确实需要查看您的生成文件才能提供帮助。如果你无法共享我理解的实际构建文件,请尝试重新编写一个简单的新项目。我很想共享我的构建文件,但有近十几个不同的文件导入到一个大型构建中。这是一个可怕的斑点,真的不应该存在,但改变是缓慢的,我的工作。你不能像两个文件重新编程?一个代表您的“主”构建文件,另一个包含项目的[cs | vb]proj?我们的构建是一个荒谬的野兽。它涉及几十个文件,包括.tasks、.targets、.proj和.components文件。它不仅执行构建,还执行网站创建、间隙、部署等。尝试分解和创建模拟场景是一个长达数周的项目。我的问题主要是为什么,即使我导入了Microsoft.WinFX.targets文件,WPF任务实际上没有被调用……我是否在错误的位置导入了.targets文件?对此是否有特殊关注?是否需要包括其他.targets文件?问题是他说同样的项目在VS2008下编译得很好\页面标记已存在。不过,我对WinFX.targets很好奇。我已经在前面的命令行生成的root build.proj文件中导入了它。WinFX.targets文件是否需要以某种方式导入才能正确应用?你能将包含WinFX.targets的整个部分(放到根目录下)发布吗?@Drew:它是在2008年构建的,但他正在将内容移动到另一个.proj文件中-所以这个特定的proj文件是不同的…最终,由VS2008构建的完全相同的.csproj文件由命令行构建。不过,VS2008会为您处理WinFX.targets的内容……这是事实
devenv.exe /build yoursolution.sln
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />