Controlling Type / Member Naming and Visibility

The x:Class token is a commonly used XAML attribute. A UserControl or Application can specify an x:
Class
attribute in its opening definition and is used to define the name of the class type in the code files.


<!-- The x:Class attribute will be used to
   Define a C# or VB class type to represent the
   code of this XAML. -->
<UserControl
x:Class = "SimpleSilverlightApplication.Page"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml">
</UserControl>


The x:ClassModifier / x:FieldModifer attributes allow you to control the visibility of a member in the related
code file. A XAML file is often paired with a C# or VB code file where you author event handlers, helper
methods, and so forth. An additional compile-time generated code file (*.g.cs / *.g.vb) will be used to contain
the XAML => code object mapping, control declarations, and more.

If you do not make use of the x:ClassModifer / x:FieldModifer attributes, the item will be defined using the
default visibility of the .NET language. In most cases, you will not need to change these defaults. As a result,
you will not frequently need to make use of the
x:ClassModifier or x:FieldModifer tokens.

As an example, however, consider the following use of the x:ClassModifier and x:FieldModifier attributes.
This will be used by msbuild.exe to declare a code file containing an internal class with a public Button.
The VB code would be similar, using the
Friend keyword rather than the C#-specific internal keyword.


<!-- This class will now be internal.
   If using a code file, the partial class must
   also be defined as internal. -->
<UserControl x:Class = "SimpleSilverlightApplication.Page"
           x:ClassModifier="internal"
  xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml">

<!-- This button will be public in the generated code file. -->
<Button
x:Name = "myButton" Content="OK" x:FieldModifier = "public"/>
</UserControl>



// C# code (VB code would be similar)
internal
partial class Page : UserControl
{
public System.Windows.Controls.Button myButton;

}


Notice that the previous <Button> has been defined using the x:Name attribute. When you are declaring an
item in XAML, you should always assign a value to the
x:Name attribute. This becomes the name of the
variable in the code file. Because the need to name a UI element is so common, you can technically omit the
x:
prefix when assigning the
Name attribute. Therefore, the previous Button could be defined as so:


<!-- No x: prefix on the Name attribute...still OK! -->
<Button Name = "myButton" Content="OK" x:FieldModifier = "public"/>
Controlling Type / Member Naming and Visibility
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services