WPF app builds in vs2k8, but not on command line w/ msbuild

夙愿已清 提交于 2019-12-12 10:59:49

问题


I have a fairly small solution that includes a WPF windows application. It builds perfectly fine when built from the solution. I recently integrated the projects contained within the solution into an existing, much larger command line build that uses MSBuild. When built from the command line, however, I get the following errors:

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

I included the WinFX targets file from .NET 3.5 in our root MSBuild .proj file, as it did not appear to be included anywhere else:

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

That did not seem to affect anything, though, and I am still encountering the errors. As far as I can tell, it appears that the custom WPF build tasks that precompile the .xaml files into .cs files, embeds and wires up resources, etc. are not running, which is why InitializeComponent and any controls defined in my views are not found. I am at a loss as to why, though...and trying to wade through the zillions of search results related to WPF and MSBuild is not getting me anywhere.

UPDATE:

Adding the Microsoft.WinFX.targets to the .csproj file seems like it would work. However, doing so causes the project to fail to build within Visual Studio 2008. Somehow, VS is including those targets for you...but I am unsure how. Does anyone know more about how VS builds WPF projects? Is there a master build file hiding somewhere that imports the appropriate targets?


回答1:


Send msbuild your solution file instead of your .csproj file can sometimes help:

msbuild.exe yoursolution.sln

Also, devenv.exe itself offers a command-line build that's supposed to be equivalent to the in-IDE experience:

devenv.exe /build yoursolution.sln



回答2:


Check out this section of the MSDN SDK called Building WPF Applications and make sure you're doing everything you need.




回答3:


Without seeing the project file directly, it's difficult to tell for sure, but it looks like your project is missing the <Page... include sections.

For example, make sure, where you define your MainWindow, that you have:

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

That, in addition to your Import of WinFX.targets, should allow this to work properly.

Right now, your errors are suggesting that the "Page" tags are missing, but the "Compile" tags for the code-behind are all in place.




回答4:


If you're asking, if visual studio works why doesn't msbuild?

Then the answer is most likely that you're using the wrong version of msbuild. You'll need msbuild 4 to build .net 4 projects.

Also see the following link http://blogs.digitaldeposit.net/saravana/post/2011/02/08/CS0103-The-name-e28098InitializeComponente28099-does-not-exist-in-the-current-context.aspx




回答5:


I realise the edits have already answered the question, but I've decided to include more info with an answer here and finished with a suggestion for your laste edited Visual Studio related question.

Edit the csproj file and put a line similar to this near the end, near to the line with "Microsoft.CSharp.targets" in it (Could be MSBuildToolsPath instead):

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

See Building a Windows Presentation Foundation Application, MSBuild Project Files for WPF and MSBuild .Targets Files.

Targets determine how projects are actually built, and depend on both properties and items. A WPF application must have both a language-specific target and a WPF-specific target:

Targets are separate files that end with the .targets extension. The target files that are included with .NET Framework 3.0 are installed in the following location:

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

The language-specific target contains the logic to build language-specific source code. The language-specific target for C# is Microsoft.CSharp.targets, and Microsoft.VisualBasic.targets for Visual Basic. Both of these targets derive from and extend the Microsoft.Common.targets target, which performs the bulk of the common, language-independent build work.

As far as "how come Visual Studio can build this" - I'd surmise that visual studio is including this behind the scenes. You could prove it to yourself one way or another by temporarily renaming C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.WinFX.target and seeing if visual studio can build anymore.



来源:https://stackoverflow.com/questions/1671032/wpf-app-builds-in-vs2k8-but-not-on-command-line-w-msbuild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!