Skip to content

Latest commit

 

History

History
58 lines (39 loc) · 4.67 KB

xfieldmodifier-directive.md

File metadata and controls

58 lines (39 loc) · 4.67 KB
title description ms.date ms.custom helpviewer_keywords ms.assetid
x:FieldModifier Directive
Learn about the x:FieldModifier Directive.
03/30/2017
devdivchpfy22
FieldModifier attribute in XAML [XAML Services]
x:FieldModifier attribute [XAML Services]
XAML [XAML Services], x:FieldModifier attribute
ed427cd4-2f35-4d24-bd2f-0fa7b71ec248

x:FieldModifier Directive

Modifies XAML compilation behavior so that fields for named object references are defined with xref:System.Reflection.TypeAttributes.Public?displayProperty=nameWithType access instead of the xref:System.Reflection.TypeAttributes.NotPublic?displayProperty=nameWithType default behavior.

XAML Attribute Usage

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

XAML Values

Value Description
Public The exact string you pass to specify xref:System.Reflection.TypeAttributes.Public?displayProperty=nameWithType versus xref:System.Reflection.TypeAttributes.NotPublic?displayProperty=nameWithType varies, depending on the code-behind programming language that is used. See Remarks.

Dependencies

If a XAML production uses x:FieldModifier anywhere, the root element of that XAML production must declare an x:Class Directive.

Remarks

x:FieldModifier is not relevant for declaring the general access level of a class or its members. It is relevant only for XAML-processing behavior when a particular XAML object that is part of a XAML production is processed, and becomes an object that is potentially accessible in the object graph of an application. By default, the field reference for such an object is kept private, which prevents control consumers from modifying the object graph directly. Instead, control consumers are expected to modify the object graph by using standard patterns that are enabled by programming models, such as by obtaining the layout root, the child element collections, the dedicated public properties, and so on.

The value for the x:FieldModifier attribute varies by programming language, and its purpose can vary in specific frameworks. The string to use depends on how each language implements its xref:System.CodeDom.Compiler.CodeDomProvider and the type converters it returns to define the meanings for xref:System.Reflection.TypeAttributes.Public?displayProperty=nameWithType and xref:System.Reflection.TypeAttributes.NotPublic?displayProperty=nameWithType, and whether that language is case sensitive.

  • For C#, the string to pass to designate xref:System.Reflection.TypeAttributes.Public?displayProperty=nameWithType is public.

  • For Microsoft Visual Basic .NET, the string to pass to designate xref:System.Reflection.TypeAttributes.Public?displayProperty=nameWithType is Public.

  • For C++/CLI, no targets for XAML currently exist; therefore, the string to pass is undefined.

You can also specify xref:System.Reflection.TypeAttributes.NotPublic?displayProperty=nameWithType (internal in C#, Friend in Visual Basic) but specifying xref:System.Reflection.TypeAttributes.NotPublic?displayProperty=nameWithType is unusual because NotPublic as the behavior is already the default.

xref:System.Reflection.TypeAttributes.NotPublic?displayProperty=nameWithType is the default behavior because it is infrequent that code outside the assembly that compiled the XAML needs access to a XAML-created element. WPF security architecture together with XAML compilation behavior will not declare fields that store element instances as public, unless you specifically set the x:FieldModifier to allow public access.

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

By default, the partial class for the root element is public; however, you can make it nonpublic by using the x:ClassModifier Directive. The x:ClassModifier Directive also affects the access level of the instance of the root element class. You can put 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 Directive.

See also