FrameworkContentElement.Name 屬性

定義

取得或設定項目的識別名稱。Gets or sets the identifying name of the element. 此名稱提供執行個體參考,以便程式設計的程式碼後置 (例如事件處理常式程式碼) 可以在 XAMLXAML 剖析期間建構之後參考項目。The name provides an instance reference so that programmatic code-behind, such as event handler code, can refer to an element once it is constructed during parsing of XAMLXAML.

public:
 property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };
[System.Windows.Localizability(System.Windows.LocalizationCategory.NeverLocalize)]
public string Name { get; set; }
member this.Name : string with get, set
Public Property Name As String

屬性值

項目的名稱。The name of the element.

實作

屬性

備註

在標記中指定 XAMLXAML 專案名稱時,此屬性最常見的用法是。The most common usage of this property is when specifying a XAMLXAML element name in markup.

這個屬性基本上會提供 WPF 架構層級的便利性屬性來設定 XAMLXAML x:Name指示詞。This property essentially provides a WPF framework-level convenience property to set the XAMLXAML x:Name Directive.

從程式碼取得 Name 並不常見,因為如果程式碼中有適當的參考,您可以只呼叫專案參考上的方法和屬性,而且通常不需要 NameGetting a Name from code is not common, because if you have the appropriate reference in code already, you can just call methods and properties on the element reference and do not generally need the Name. 如果字串有一些多載意義,例如在 UIUI中顯示該名稱很有用時,就會發生例外狀況。An exception is if the string has some overloaded meaning, for instance if it is useful to display that name in UIUI. 如果原本的 Name 是從標記設定的,也不建議從程式碼設定 Name,而且變更屬性不會變更物件參考。Setting a Name from code if the original Name was set from markup is also not recommended, and changing the property will not change the object reference. 只有在 XAMLXAML 載入期間明確建立基礎名稱範圍時,才會建立這類物件參考。Such object references are created only when the underlying namescopes are explicitly created during XAMLXAML loading.

您必須明確地呼叫 RegisterName,以便對已載入專案的 Name 屬性進行有效的變更。You must specifically call RegisterName to make an effective change on the Name property of an already loaded element.

其中一個值得注意的情況,就是從程式碼設定 Name 很重要的,就是要針對腳本執行的命名元素。One notable case where setting Name from code is important is for naming elements that storyboards will run against. 在您可以註冊名稱之前,可能也需要具現化並指派 NameScope 實例。Before you can register a name, might also need to instantiate and assign a NameScope instance. 請參閱範例一節或分鏡腳本總覽See the Example section, or Storyboards Overview.

從程式碼設定 Name 有限制的應用程式,但依名稱查閱元素較常見,特別是當您採用流覽模型,其中頁面重載至應用程式,而執行時間程式碼不一定是該專案的程式碼後置個別頁面。Setting Name from code has limited applications, but looking up an element by name is more common, particularly if you are employing a navigation model where pages reload into the application and the run-time code is not necessarily the code-behind of that respective page. 公用程式方法 FindName(可從任何 FrameworkContentElement取得)可以在該專案的邏輯樹狀結構中以遞迴方式 Name 來尋找任何元素。The utility method FindName, which is available from any FrameworkContentElement, can find any element by Name in that element's logical tree recursively. 或者,您可以使用 LogicalTreeHelperFindLogicalNode 靜態方法,這也會採用 Name 字串做為引數。Or you can use the FindLogicalNode static method of LogicalTreeHelper, which also takes the Name string as an argument.

常用的根項目(例如WindowPage)會執行介面 INameScopeCommonly used root elements (Window, Page for example) implement the interface INameScope. 此介面的執行應該會強制其範圍內的名稱明確。Implementations of this interface are expected to enforce that names be unambiguous within their scope.

相依性屬性資訊Dependency Property Information

識別碼欄位Identifier field NameProperty
設定為 true 的中繼資料屬性Metadata properties set to true IsAnimationProhibited

適用於

另請參閱