我有一个相当小的解决方案,包括一个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文件似乎可以正常工作。但是,这样做会导致项目无法在Visual Studio 2008中构建。不知何故,VS会为您包含这些目标......但我不确定如何。有谁知道VS如何构建WPF项目?是否存在隐藏某个导入适当目标的主构建文件?
答案 0 :(得分:5)
发送msbuild您的解决方案文件而不是.csproj文件有时可以提供帮助:
msbuild.exe yoursolution.sln
此外,devenv.exe本身提供的命令行构建应该等同于in-IDE体验:
devenv.exe /build yoursolution.sln
答案 1 :(得分:2)
查看名为Building WPF Applications的MSDN SDK的此部分,确保您正在执行所需的一切。
答案 2 :(得分:1)
如果没有直接查看项目文件,很难确定,但看起来您的项目缺少<Page...
包含部分。
例如,确保您定义MainWindow的位置:
<ItemGroup>
<Page Include="MainWindow.xaml" />
<Compile Include="MainWindow.xaml.cs" />
... Other items...
除了导入WinFX.targets之外,还应该允许它正常工作。
目前,您的错误表明缺少“Page”标记,但代码隐藏的“编译”标记都已到位。
答案 3 :(得分:1)
然后答案很可能是你正在使用错误版本的msbuild 。 你需要msbuild 4来构建.net 4项目。
答案 4 :(得分:0)
我意识到这些编辑已经回答了这个问题,但我决定在这里添加更多信息和答案,并为您编辑的Visual Studio相关问题提出建议。
编辑csproj文件并在其末尾附近放置一行类似于“Microsoft.CSharp.targets”的行(可能是MSBuildToolsPath):
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
请参阅Building a Windows Presentation Foundation Application,MSBuild Project Files for WPF和MSBuild .Targets Files。
目标确定项目的方式 实际建成,并依赖于两者 属性和项目。一个WPF 申请必须同时具备 语言特定的目标和 WPF特定目标:
目标是结束的单独文件 扩展名为.targets。该 目标文件包含在 .NET Framework 3.0已安装在 以下位置:
%WINDIR%\ Microsoft.NET \框架\ vX.X.X
特定于语言的目标包含 构建语言特定的逻辑 源代码。语言特定的 C#的目标是 Microsoft.CSharp.targets,和 Microsoft.VisualBasic.targets for Visual Basic。这两个目标 源自并扩展 Microsoft.Common.targets目标,其中 执行大部分常见的, 与语言无关的构建工作。
至于“Visual Studio如何构建这个” - 我猜测视觉工作室将在幕后加入。您可以通过暂时重命名C:\ Windows \ Microsoft.NET \ Framework \ v3.5 \ Microsoft.WinFX.target并查看visual studio是否可以构建来以这种或那种方式证明这一点。