Xamarin 中的 XAML 欄位修飾詞XAML Field Modifiers in Xamarin.Forms

x:FieldModifier Namespace 屬性會為已命名的 XAML 元素指定產生之欄位的存取層級。The x:FieldModifier namespace attribute specifies the access level for generated fields for named XAML elements. 屬性的有效值為:Valid values of the attribute are:

  • private–指定 XAML 元素的產生欄位只能在其宣告所在的類別主體中存取。private – specifies that the generated field for the XAML element is accessible only within the body of the class in which it is declared.
  • public–指定針對 XAML 元素產生的欄位沒有存取限制。public – specifies that the generated field for the XAML element has no access restrictions.
  • protected–指定 XAML 專案的產生欄位可以在其類別和衍生類別實例之間存取。protected – specifies that the generated field for the XAML element is accessible within its class and by derived class instances.
  • internal–指定針對 XAML 元素產生的欄位只能在相同元件的類型內進行存取。internal – specifies that the generated field for the XAML element is accessible only within types in the same assembly.
  • notpublic–指定針對 XAML 元素產生的欄位只能在相同元件的類型內進行存取。notpublic – specifies that the generated field for the XAML element is accessible only within types in the same assembly.

根據預設, 如果未設定屬性的值, 則為元素產生的欄位將會是privateBy default, if the value of the attribute isn't set, the generated field for the element will be private.

注意

屬性的值可以使用任何大小寫, 因為它將會以 Xamarin 格式轉換成小寫。The value of the attribute can use any casing, as it will be converted to lowercase by Xamarin.Forms.

若要處理x:FieldModifier屬性, 必須符合下列條件:The following conditions must be met for an x:FieldModifier attribute to be processed:

  • 最上層的 XAML 元素必須是有效x:Class的。The top-level XAML element must be a valid x:Class.
  • 目前的 XAML 元素具有指定x:Name的。The current XAML element has an x:Name specified.

下列 XAML 顯示設定屬性的範例:The following XAML shows examples of setting the attribute:

<Label x:Name="privateLabel" />
<Label x:Name="internalLabel" x:FieldModifier="internal" />
<Label x:Name="publicLabel" x:FieldModifier="public" />

重要

x:FieldModifier屬性不能用來指定 XAML 類別的存取層級。The x:FieldModifier attribute cannot be used to specify the access level of a XAML class.