-1

This is a continuation of my thread:

WPF: How to handle multi-XAML UIs? (Follow-Up)

I am curious as to the parent-child relationship between an abc.xaml file and its corresponding abc.xaml.cs. How is the relationship implemented? Why does one look under/inside the other in MSVS? How would I create those two files with Notepad? Is it simply a matter of naming them with a common basename?

TIA.

Community
  • 1
  • 1
Travis Banger
  • 697
  • 1
  • 7
  • 19
  • 1
    Do not post separate question. – Rohit Vats Dec 07 '13 at 18:03
  • 1
    @RohitVats despite the mention to other question, this is a separate question in itself about a completely different matter. – Federico Berasategui Dec 07 '13 at 19:43
  • @HighCore is correct. In fact, I experimented with Notepad and the answer to `this.` (pun intended :-) post is: "Yes, the files are considered hierarchically linked basely solely on the fact that they share the basename". I wrote a couple minimal 2-liners (a `.xaml` and a `.xaml.cs` and MSVS seemed to be completely persuaded of their legitimacy. However, I have absolutely no clue about the related question: which remins unanswered. Perhaps what I want is not possible? – Travis Banger Dec 07 '13 at 20:31
  • @TravisBanger no the file names have nothing to do with anything. Please see my answer. A file is dependent upon other file in a .Net project if it's `ProjectItem` is marked as `[DependentUpon]` in the Project file. – Federico Berasategui Dec 07 '13 at 21:32

1 Answers1

2

I wouldn't worry about that too much if I were you. It's a completely IDE-related matter that does not really affect you in any way.

Yet, let me explain that to you real quick:

When you add a UserControl or Window in a WPF Project by using the Visual Studio Add -> New Item.. action, Visual Studio adds 3 files to your project:

1 - MyUserControl.xaml Is the XAML file, which contains the XAML that comprises your UserControl.

2 - MyUserControl.xaml.cs, which is the "code behind" file, that defines a public partial class derived from System.Windows.Controls.UserControl, which allows you to create additional logic for the interaction with your UserControl.

3 - Under the covers, another file called MyUserControl.g.i.cs is also generated. This file contains the same class as #2, also defined as partial, and additionally contains the definition of all the fields inside the class for any relevant XAML-defined elements that were given an x:Name.

This means that if your XAML contains something like this:

<TextBox x:Name="TextBox1"/>

the .g.i.cs file will contain a declaration like this:

internal System.Windows.Controls.TextBox TextBox1;

the value of this field is assigned via WPF's internal mechanisms that I will not discuss right now.

The bottom line is that the code you may write in the MyUserControl.xaml.cs code behind file is probably going to make use of the class members defined in .g.i.cs (such as TextBox1), and since that file is dependent on the XAML file, so is your code behind file.

That's why the code behind file is set as DependentUpon the XAML file in the Project Schema, which if you look at the csproj file, looks something like this:

<!-- ... -->
<ItemGroup>
    <Page Include="MyUserControl.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
    </Page>
</ItemGroup>

<ItemGroup>
    <Compile Include="MyUserControl.xaml.cs">
        <DependentUpon>MyUserControl.xaml</DependentUpon>
        <!-- notice how this file is defined as DepedentUpon the XAML file -->
    </Compile>
</ItemGroup>

<!-- ... -->
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154