Freigeben über


x:FieldModifier Attribute

Modifies XAML compilation behavior, such that fields for named object references are defined with NonPublic access rather than the Public default behavior.

XAML Attribute Usage

<object x:FieldModifier="Public".../>

XAML Values

Public

The exact string to pass to specifiy Public versus NonPublic varies, depending on the code-behind programming language being used. See Remarks.

Dependencies

x:Name Attribute must also be provided on the same element.

Remarks

The value for the x:FieldModifier attribute will vary by programming language. The string to use will depend on how each language implements its CodeDomProvider and the type converters it returns to define the meanings for TypeAttributes.Public and TypeAttributes.NonPublic, and whether that language is case sensitive or not.

  • For C#, the string to pass to designate Public is public.

  • For Microsoft Visual Basic .NET, the string to pass to designate Public is Public.

  • For C++/CLI, the C++/CLI targets do not currently support compiling XAML.

You can also specify NonPublic (internal in C#, Friend in Microsoft Visual Basic .NET) but that is uncommon, because NonPublic as the behavior is already the default.

NonPublic is the default because it is uncommon that code outside of the assembly that compiled the XAML would have any need to access a XAML created element. The WPF security architecture makes the conscious choice to not make fields that store element instances public, unless you specifically set the x:FieldModifier.

x:FieldModifier is only relevant for elements with an x:Name Attribute, because that name is used to reference the field once it is public.

The partial class for the root element is public by default, but can be made nonpublic by using the x:ClassModifier Attribute. The x:ClassModifier Attribute also affects the access level of the instance of the root element class. You can place both x:Name and x:FieldModifier on the root element, but this only makes a public field copy of the root element, with the true root element class access level still controlled by x:ClassModifier Attribute.

See Also

Reference

x:Name Attribute
x:ClassModifier Attribute

Concepts

XAML and Custom Classes
Code-Behind and XAML
Building a Windows Presentation Foundation Application