DependencyObject 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表參與依賴屬性系統的物件。 DependencyObject 是許多重要 UI 相關類別的直接基底類別,例如 UIElement、 Geometry、 FrameworkTemplate、 Style 和 ResourceDictionary。 想了解更多 DependencyObject 如何支援相依屬性的資訊,請參閱 相依屬性概覽。
public ref class DependencyObject
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class DependencyObject
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class DependencyObject
Public Class DependencyObject
- 繼承
- 衍生
- 屬性
Windows 需求
| 規格需求 | Description |
|---|---|
| 裝置系列 |
Windows 10 (已於 10.0.10240.0 引進)
|
| API contract |
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)
|
範例
此範例定義了一個源自 DependencyObject 的類別,並定義了一個附加屬性及識別碼欄位。 這個類別的情境是,它是一個服務類別,宣告一個附加屬性,其他 UI 元素可以在 XAML 中設定。服務在執行時可能會對這些 UI 元素附加的屬性值進行行動。
public abstract class AquariumServices : DependencyObject
{
public enum Buoyancy {Floats,Sinks,Drifts}
public static readonly DependencyProperty BuoyancyProperty = DependencyProperty.RegisterAttached(
"Buoyancy",
typeof(Buoyancy),
typeof(AquariumServices),
new PropertyMetadata(Buoyancy.Floats)
);
public static void SetBuoyancy(DependencyObject element, Buoyancy value)
{
element.SetValue(BuoyancyProperty, value);
}
public static Buoyancy GetBuoyancy(DependencyObject element)
{
return (Buoyancy)element.GetValue(BuoyancyProperty);
}
}
Public Class AquariumServices
Inherits DependencyObject
Public Enum Buoyancy
Floats
Sinks
Drifts
End Enum
Public Shared ReadOnly BuoyancyProperty As DependencyProperty = _
DependencyProperty.RegisterAttached(
"Buoyancy", _
GetType(Buoyancy), _
GetType(AquariumServices), _
New PropertyMetadata(Buoyancy.Floats))
Public Sub SetBuoyancy(element As DependencyObject, value As Buoyancy)
element.SetValue(BuoyancyProperty, value)
End Sub
Public Function GetBuoyancy(element As DependencyObject) As Buoyancy
GetBuoyancy = CType(element.GetValue(BuoyancyProperty), Buoyancy)
End Function
End Class
public static bool ClearSetProperty(DependencyObject targetObject, DependencyProperty targetDP)
{
if (targetObject == null || targetDP == null)
{
throw new ArgumentNullException();
}
object localValue = targetObject.ReadLocalValue(targetDP);
if (localValue == DependencyProperty.UnsetValue)
{
return false;
}
else
{
targetObject.ClearValue(targetDP);
return true;
}
}
Public Shared Function ClearSetProperty(targetObject As DependencyObject, targetDP As DependencyProperty) As Boolean
If targetObject Is Nothing Or targetDP Is Nothing Then
Throw New ArgumentNullException()
End If
Dim localValue As Object = targetObject.ReadLocalValue(targetDP)
If localValue = DependencyProperty.UnsetValue Then
ClearSetProperty = False
Else
targetObject.ClearValue(targetDP)
ClearSetProperty = True
End If
End Function
此範例展示了一個簡單的相依屬性宣告。 呼叫 GetValue 即構成新相依屬性封裝器的 get accessor 實作的全部。 呼叫 SetValue 即構成整個 集合 存取器實作。 更多範例請參見 自訂相依屬性。
public class Fish : Control
{
public static readonly DependencyProperty SpeciesProperty =
DependencyProperty.Register(
"Species",
typeof(String),
typeof(Fish), null
);
public string Species
{
get { return (string)GetValue(SpeciesProperty); }
set { SetValue(SpeciesProperty, (string)value); }
}
}
Public Class Fish
Inherits Control
Public Shared ReadOnly SpeciesProperty As DependencyProperty = _
DependencyProperty.Register(
"Species", _
GetType(String), _
GetType(Fish), _
Nothing)
Public Property Species As String
Get
Species = CType(GetValue(SpeciesProperty), String)
End Get
Set(value As String)
SetValue(SpeciesProperty, value)
End Set
End Property
End Class
備註
DependencyObject 類別使其多個衍生類別能夠提供依賴屬性系統服務。 欲了解更多依賴性質概念,請參閱 依賴性質概述。
相依屬性系統的主要功能是計算屬性的值,並提供系統關於變更值的通知。 另一個參與依賴屬性系統的關鍵類別是 DependencyProperty。 DependencyProperty 允許將相依屬性註冊到屬性系統中,而 DependencyObject 作為基底類別則允許物件使用並設定相依屬性。
以下是 DependencyObject 提供或支援的一些重要服務與特性:
- 依賴屬性託管支援現有 Windows 執行階段 相依屬性。
- 自訂相依物業託管支援。 你透過呼叫 Register 方法,並將該方法的回傳值存為 DependencyObject 類別中的公開靜態屬性來註冊相依屬性。
- 附加屬性主機支援現有 Windows 執行階段 附加屬性。
- 自訂附設的物業託管支援。 你可以透過呼叫 RegisterAttaded 方法,並將該方法的回傳值存為類別中的公開靜態屬性來註冊相依屬性。
- 取得 並 設定 任何存在於 DependencyObject 上的相依屬性值的實用性方法。 你可以在定義自訂相依屬性「包裝器」時使用這些,也可以從應用程式程式碼中作為現有「包裝者」屬性的替代方案。
- 用於檢視元資料或屬性值的進階情境工具(例如 GetAnimationBaseValue)。
- 強制執行所有 DependencyObject 實例與 Windows 執行階段 主 UI 執行緒的關聯性。
- Dispatcher 屬性用於進階執行緒情境。 取得 Dispatcher 值後,會參考 CoreDispatcher 物件。 有了 CoreDispatcher,工作執行緒可以執行使用 DependencyObject 但不在 UI 執行緒上的程式碼,因為 CoreDispatcher 可以將執行延後給一個不會阻塞或干擾 UI 執行緒的非同步操作。 請參見下方「DependencyObject 與執行緒」章節。
- 基本的資料綁定與樣式支援,透過將屬性設定為表達式,以便在物件生命週期的後期評估。 這些概念在依賴 性質概述中有更詳細的說明。 另見 資料綁定深入說明。
DependencyObject 與執行緒
所有 DependencyObject 實例必須建立在 與該應用程式目前 視窗相關聯的 UI 執行緒上。 這是系統強制執行的,對你的程式碼有兩個重要影響:
- 使用兩個 DependencyObject 實例 API 的程式碼,永遠會在同一個執行緒上執行,也就是 UI 執行緒。 這種情況下通常不會遇到執行緒問題。
- 未在主 UI 執行緒執行的程式碼無法直接存取 DependencyObject,因為 DependencyObject 僅與 UI 執行緒有關聯。 只有在 UI 執行緒上執行的程式碼才能更改或讀取相依屬性的值。 例如,你用 .NET 任務或明確的 ThreadPool 執行緒啟動的工作執行緒,將無法讀取相依屬性或呼叫其他 API。
你並沒有完全被阻擋使用工作執行緒中的 DependencyObject。 但你必須從 DependencyObject 取得 CoreDispatcher 物件( DependencyObject.Dispatcher 的值),才能跨越應用程式介面執行緒與系統上其他執行緒之間刻意分離的區域。 CoreDispatcher 會暴露 RunAsync 方法。 呼叫 RunAsync 來執行你期待的程式碼( IAsyncAction)。 如果是簡單的程式碼,你可以用 lambda 運算式,否則你可以實作為代理(DispatchedHandler)。 系統會決定你的程式碼可以執行的時間。 因為它能跨執行緒存取, DependencyObject.Dispatcher 是唯一一個可以從非 UI 執行緒存取且不會拋出跨執行緒例外的 DependencyObject 或其子類別的實例 API。 其他所有 DependencyObject API 若嘗試從工作執行緒或其他非 UI 執行緒呼叫,都會拋出例外。
執行緒問題通常可以在典型的 UI 程式碼中避免。 不過,裝置通常不會與 UI 執行緒相關聯。 如果你是用裝置取得的資訊即時更新 UI,通常必須取得 CoreDispatcher 才能更新 UI。 服務是另一種情況,你用來存取服務的程式碼可能沒有在 UI 執行緒上執行。
例如,如果你自己定義 DependencyObject 類型並嘗試用於資料來源,可能會遇到 DependencyObject 相關的執行緒問題,或其他情況中 DependencyObject 不一定適合(因為該物件與 UI 沒有直接關聯)。 例如,你可能會嘗試對背景執行緒或其他工作執行緒進行效能優化,這些執行緒在呈現前或回應裝置、服務或其他外部輸入時會改變物件的值。 評估你是否真的需要依賴性質來滿足你的情境;也許標準屬性就足夠了。
DependencyObject 衍生類別
DependencyObject 是多個直接衍生的類別的父類別,這些類別都是你用於應用程式及其 XAML 介面的程式模型基礎。 以下是一些著名的衍生類別:
- UIElement:大多數能處理 UI 輸入的視覺物件的基底類別。 FrameworkElement 在這個階層中更進一步,Control 也是如此,因此在 Windows 執行階段 中有數百個控制項和其他類別,它們的類別階層中都有 UIElement 類別。
- XAML 風格系統支援類別: FrameworkTemplate ( ControlTemplate、 DataTemplate、 ItemsPanelTemplate 的父)、 Style、 SetterBase ( Setter 的父)、 ToolTipTemplateSettings 及其他 *TemplateSettings。
- 視覺狀態模型: Transition、 VisualState、 VisualStateGroup、 VisualStateManager、 VisualTransition。
- 資料綁定支援: BindingBase ( 綁定的父節點)、 CollectionViewSource、 PropertyPath、 RelativeSource。
- 分鏡動畫: Timeline (分 鏡的父節點、打字動畫、主題動畫)、打字關鍵影格(像 ColorKeyFrame)、 KeySpline、 EasingFunctionBase。
- XAML 圖形與渲染原語: 筆刷、 幾何、 通用變換、 漸層停止、 路徑圖形、 路徑段、 投影。
- 影像與渲染表面互通: ImageSource ( BitmapSource、 RenderTargetBitmap、 SurfaceImageSource 等的父程式)。
- 視窗:代表主要應用程式視窗。
- FlyoutBase ( Flyout 與 MenuFlyout 的母公司)。
- ResourceDictionary:在 XAML 中定義有鍵的資源。
- TextElement:Block 和 Inline 的父,這些又是用於 TextBlock 內容或富文本元素的文字元素的基底類別。
- AutomationPeer:Microsoft 使用者介面自動化對等節點的基底類別,代表 XAML 控制項給Microsoft 使用者介面自動化監聽者。 還有, IRawElementProviderSimple。
- 其他: CacheMode、 ColumnDefinition、 DependencyObjectCollection、 InputScope、 InputScopeName、 PageStackEntry、 PrintDocument、 RowDefinition、 TimelineMarker、 TriggerAction、 TriggerBase。
建構函式
| 名稱 | Description |
|---|---|
| DependencyObject() |
提供 DependencyObject 衍生類別的基底類別初始化行為。 |
屬性
| 名稱 | Description |
|---|---|
| Dispatcher |
取得該物件所關聯的核心 派遣器 。 CoreDispatcher 代表一個設施,即使程式碼是由非 UI 執行緒發起,也能存取 UI 執行緒上的 DependencyObject。 |
方法
| 名稱 | Description |
|---|---|
| ClearValue(DependencyProperty) |
清除依賴性財產的局部價值。 |
| GetAnimationBaseValue(DependencyProperty) |
回傳任何為相依屬性建立的基礎值,適用於動畫未啟用時。 |
| GetValue(DependencyProperty) |
回傳 DependencyObject 中相依屬性的當前有效值。 |
| ReadLocalValue(DependencyProperty) |
若設定了本地值,則回傳依賴性質的局部值。 |
| RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback) |
註冊一個通知函式,用於監聽此 DependencyObject 實例中特定 DependencyProperty 的變更。 |
| SetValue(DependencyProperty, Object) |
將相依屬性的局部值設定在 DependencyObject 上。 |
| UnregisterPropertyChangedCallback(DependencyProperty, Int64) |
取消先前透過呼叫 RegisterPropertyChangedCallback 註冊的變更通知。 |