views:

472

answers:

4

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  A: 

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

Drew Marsh
I did read that article before I posted my question. That was how I learned about Microsoft.WinFX.targets. The build still doesn't work at the command line with msbuild, however. Very confusing problem.
jrista
Hmmm, I mean it's clearly not processing the XAML correctly. It should be finding your <Page> items and processing them. Are those the only errors/warnings from MSBuild? Maybe throw the /verbosity:detailed switch on the call to MSBuild to see if there's something silently failing under the scenes.
Drew Marsh
Yeah, those are the only errors. There are no other warnings...the project has zero warnings.
jrista
A: 

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.

Reed Copsey
The problem is he says the same project compiles just fine under VS2008. :\
Drew Marsh
The Page tags are present. I am curious about the WinFX.targets, though. I have imported it, earlier on in the root build .proj file for the command line build. Does the WinFX.targets file need to be imported in a certain way to be properly applied?
jrista
Can you post the whole section (to the root) where you're including the WinFX.targets?
Reed Copsey
@Drew: It builds in 2008, but he's moving stuff into a different .proj file - so this specific proj file isn't the same...
Reed Copsey
Ultimately, the same exact .csproj file that is built by VS2008 is built by the command line build. However, VS2008 takes care of the WinFX.targets stuff for you...it is not imported directly by the .csproj. However, I have added an import for the WinFX.targets file, and the command line build STILL is not building the WPF specific targets. That is where my confusion lies...
jrista
+3  A: 

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
Andrew Arnott
A: 

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.

paulecoyote