Bobby Tables
Bobby Tables

Reputation: 3013

Updated custom generated files on save

In a .xaml file as soon as I add a new control, I am able to reference it for the xaml.cs file because VS is kind enough the generate the C# code for it in a g.i.cs file.

For example

In MainWindow.xaml I add

<Grid>
    <Button x:Name="Test"/>
</Grid>

I am able to access it from MainWindow.xaml.cs like

this.Test.Content = "TestButton";

Because VS generated MainWindow.g.i.cs in the obj folder which contains

internal System.Windows.Controls.Button Test;

I have been able to have exactly the same behavior using msbuild for a custom file .xyz which is a designer view for a .cs file. I've have created a msbuild target that regenerates the .cs files on build and on project load but I'm not able to do the same thing on file save

Now my questions:

  1. How does VS regenerate the g.i.cs on save ?
  2. Is this a VS feature or an msbuild one ?
  3. Can I trigger an incremental build on save?

Update:

I've also looked through a WPF .csproj and it uses the same .targets files as a class library C# project. The only differences I could spot are the DependentUpon and the SubType item metadata

Upvotes: 0

Views: 821

Answers (1)

Leo Liu
Leo Liu

Reputation: 76986

  1. How does VS regenerate the g.i.cs on save ?

AFAIK, Visual Studio invoke the XAML compiler xamlc.exe to compile the XAML files regenerate the g.i.cs on save.

Check this blog Understanding In and Out of XAML in WPF:

In the blog, the author said that compilation of XAML happened in 2 steps:

Step 1. The first step is to compile the XAML files into BAML using the xamlc.exe compiler. For example, if our project includes a file name Window1.xaml, the compiler will create a temporary file named Window1.baml and place it in the obj\Debug subfolder (in our project folder). At the same time, a partial class is created for our window, using the language of our choice. For example, if we’re using C#, the compiler will create a file named Window1.g.cs in the obj\Debug folder. The g stands for generated.

Step 2. When the XAML-to-BAML compilation stage is finished, Visual Studio uses the appropriate language compiler to compile our code and the generated partial class files. In the case of a C# application, it’s the csc.exe compiler that handles this task. The compiled code becomes a single assembly Window1.exe) and the BAML for each window is embedded as a separate resource.

.

  1. Is this a VS feature or an msbuild one?

This should be Visual Studio feature. As I said above, Visual Studio invoke the XAML compiler xamlc.exe to compile the .xaml files on save. Visual Studio will invoke MSBuild to compile the those file to .exe/.dll on build. Besides, when you on save not build the project, you can open your Task Manager, there is no MSBuild.exe exists, only when you build the project, it will comes up. So this should be Visual Studio feature not MSBuild on save.

  1. Can I trigger an incremental build on save?

In the Visual Studio, the build system provides support for incremental builds. But if you want trigger an incremental build with your a custom MSBuild file .xyz, I am afraid you could not do that, because MSBuild only works on Build not save.

The [!INCLUDETLA2#tla_wpf] build system provides support for incremental builds. It is fairly intelligent about detecting changes made to markup or code, and it compiles only those artifacts affected by the change. The incremental build mechanism uses the following files

Check the Building a WPF Application (WPF) for more details.

Hope this helps.

Upvotes: 3

Related Questions