【问题标题】:WPF app builds in vs2k8, but not on command line w/ msbuildWPF 应用程序在 vs2k8 中构建,但不在带有 msbuild 的命令行上
【发布时间】:2009-11-04 00:29:11
【问题描述】:

我有一个相当小的解决方案,其中包括一个 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 项目的信息?是否有一个隐藏在某个地方的主构建文件可以导入适当的目标?

【问题讨论】:

  • 我很乐意在这方面为您提供更多帮助,但我确实需要查看您的构建文件才能提供帮助。如果您无法共享我理解的实际构建文件,请尝试使用一个简单的新项目进行复制。
  • 我很想分享我的构建文件,但是有将近十几个不同的文件导入到一个庞大的构建中。它是一个可怕的斑点,甚至不应该存在,但我工作的地方变化很慢。
  • 你不能复制两个文件?一个代表您的“主要”构建文件,另一个包括项目的 .[cs|vb]proj?
  • 我们的构建是一头荒谬的野兽。它涉及数十个文件,包括 .tasks、.targets、.proj 和 .components 文件。它不仅执行构建,还执行网站创建、GACing、部署等。尝试分解和创建模拟场景是一个长达数周的项目。我的问题主要是关于为什么,即使我已经导入了 Microsoft.WinFX.targets 文件,但实际上并没有调用 WPF 任务......我是否将 .targets 文件导入了错误的位置?有什么特别的担忧吗?是否还有其他需要包含的 .targets 文件?等等

标签: wpf .net-3.5 msbuild msbuild-task


【解决方案1】:

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

msbuild.exe yoursolution.sln

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

devenv.exe /build yoursolution.sln

【讨论】:

  • 构建服务器没有安装Visual Studio
  • @Lavinski 你最终可能不得不重新考虑这一点。例如,如果您希望构建服务器能够构建 Web 应用程序或 MSTest 单元测试项目,我所知道的安装必要构建工具的唯一方法是在构建服务器上安装 VS。
【解决方案2】:

查看名为 Building WPF Applications 的 MSDN SDK 部分,并确保您正在做您需要的一切。

【讨论】:

  • 在发布我的问题之前,我确实阅读了那篇文章。这就是我了解 Microsoft.WinFX.targets 的方式。但是,该构建仍然无法在带有 msbuild 的命令行中运行。非常混乱的问题。
  • 嗯,我的意思是它显然没有正确处理 XAML。它应该找到您的 项目并处理它们。这些是来自 MSBuild 的唯一错误/警告吗?或许可以在调用 MSBuild 时将 /verbosity:detailed 开关打开,看看是否有什么东西在幕后默默地失败了。
  • 是的,这些是唯一的错误。没有其他警告...该项目的警告为零。
【解决方案3】:

如果不直接查看项目文件,很难确定,但您的项目似乎缺少 &lt;Page... 包含部分。

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

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

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

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

【讨论】:

  • 问题是他说同一个项目在VS2008下编译得很好。 :\
  • 页面标签存在。不过,我对 WinFX.targets 很好奇。我已经在命令行构建的根构建 .proj 文件中导入了它。 WinFX.targets文件是否需要以某种方式导入才能正确应用?
  • 您能否将包含 WinFX.targets 的整个部分(到根目录)发布?
  • @Drew:它建于 2008 年,但他正在将东西移动到不同的 .proj 文件中 - 所以这个特定的 proj 文件不一样...
  • 最终,由 VS2008 构建的完全相同的 .csproj 文件由命令行构建构建。但是,VS2008 会为您处理 WinFX.targets 的内容……它不是由 .csproj 直接导入的。但是,我已经为 WinFX.targets 文件添加了一个导入,并且命令行构建仍然没有构建 WPF 特定的目标。这就是我的困惑所在......
【解决方案4】:

如果您要问,如果 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

【讨论】:

  • 那是在我一直工作的公司开始转向 VS2010 和 .NET 4 之前的一段时间。当时一切都是 .NET 3.5,包括命令行工具。
  • @jrista 这个答案不适合你,但不会出错。瓷砖与我遇到的问题相匹配,这个答案解决了它,所以我说它是有效的。所以去投票吧。
【解决方案5】:

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

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

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

请参阅Building a Windows Presentation Foundation ApplicationMSBuild Project Files for WPFMSBuild .Targets Files

目标决定项目如何进行 实际构建,并依赖于两者 属性和项目。一个WPF 应用程序必须同时具有 特定语言的目标和 WPF 特定目标:

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

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

特定语言的目标包含 构建特定语言的逻辑 源代码。特定语言 C# 的目标是 Microsoft.CSharp.targets 和 Microsoft.VisualBasic.targets 为 视觉基础。这两个目标 源自并扩展 Microsoft.Common.targets 目标,其中 执行大部分常见操作, 独立于语言的构建工作。

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

【讨论】:

    猜你喜欢
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    相关资源
    最近更新 更多