5

我有一个相当小的解决方案,其中包括一个 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

我将 .NET 3.5 中的 WinFX 目标文件包含在我们的根 MSBuild .proj 文件中,因为它似乎没有包含在其他任何地方:

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

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

更新:

将 Microsoft.WinFX.targets 添加到 .csproj 文件似乎可行。但是,这样做会导致项目无法在 Visual Studio 2008 中构建。不知何故,VS 为您包含了这些目标......但我不确定如何。有谁知道更多关于 VS 如何构建 WPF 项目的信息?是否有一个隐藏在某个地方的主构建文件可以导入适当的目标?

4

5 回答 5

5

发送 msbuild 您的解决方案文件而不是您的 .csproj 文件有时会有所帮助:

msbuild.exe yoursolution.sln

此外,devenv.exe 本身提供了一个命令行构建,它应该等同于 in-IDE 体验:

devenv.exe /build yoursolution.sln
于 2009-11-11T10:05:13.800 回答
2

查看 MSDN SDK 中称为“构建 WPF 应用程序”的这一部分,并确保您正在做所有您需要的事情。

于 2009-11-04T01:38:19.793 回答
1

如果不直接查看项目文件,很难确定,但看起来您的项目缺少<Page...包含部分。

例如,确保在您定义 MainWindow 的位置具有:

<ItemGroup>
   <Page Include="MainWindow.xaml" />
   <Compile Include="MainWindow.xaml.cs" />
   ... Other items...

除了您的 WinFX.targets 导入之外,这应该允许它正常工作。

现在,您的错误表明缺少“页面”标签,但代码隐藏的“编译”标签都已到位。

于 2009-11-09T19:14:50.373 回答
1

如果您要问,如果 Visual Studio 工作正常,为什么 msbuild 不工作?

那么答案很可能是您使用了错误版本的 msbuild。您需要 msbuild 4 来构建 .net 4 项目。

另请参阅以下链接http://blogs.digitaldeposit.net/saravana/post/2011/02/08/CS0103-The-name-e28098InitializeComponente28099-does-not-exist-in-the-current-context.aspx

于 2011-06-02T01:50:12.150 回答
0

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

编辑 csproj 文件并在末尾附近放置与此类似的行,靠近其中包含“Microsoft.CSharp.targets”的行(可能是 MSBuildToolsPath):

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

请参阅构建 Windows Presentation Foundation 应用程序用于 WPF 的 MSBuild 项目文件MSBuild .Targets 文件

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

目标是以 .targets 扩展名结尾的单独文件。.NET Framework 3.0 中包含的目标文件安装在以下位置:

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

特定于语言的目标包含构建特定于语言的源代码的逻辑。C# 的语言特定目标是 Microsoft.CSharp.targets,Visual Basic 是 Microsoft.VisualBasic.targets。这两个目标都派生自并扩展了 Microsoft.Common.targets 目标,该目标执行大部分通用的、独立于语言的构建工作。

至于“Visual Studio 怎么能构建这个”——我推测 Visual Studio 在幕后包含了这个。您可以通过暂时重命名 C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.WinFX.target 并查看 Visual Studio 是否可以再构建来以一种或另一种方式向自己证明这一点。

于 2010-07-27T14:39:41.973 回答