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 要求
| 要求 | 说明 |
|---|---|
| 设备系列 |
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 访问器实现。 对 SetValue 的调用构成了 set 访问器实现的整个部分。 有关更多示例,请参阅 自定义依赖项属性。
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 运行时附加属性的支持。
- 自定义附加属性托管支持。 通过调用 RegisterAttached 方法并将方法的返回值存储为类中的公共静态属性,为附加属性用法注册依赖属性。
- 获取 和 设置 DependencyObject 上存在的任何依赖属性的值的实用工具方法。 在定义自定义依赖属性“包装器”时使用这些属性,还可以将其从应用代码用作使用现有“包装器”属性的替代方法。
- 用于检查元数据或属性值的高级方案实用工具(例如 GetAnimationBaseValue)。
- 对所有 DependencyObject 实例执行线程关联到Windows 运行时的主 UI 线程。
- 高级线程方案的 Dispatcher 属性。 获取 Dispatcher 值提供对 CoreDispatcher 对象的引用。 借助 CoreDispatcher,工作线程可以运行使用 DependencyObject 但不在 UI 线程上的代码,因为 CoreDispatcher 可以将执行推迟到不会阻止或干扰 UI 线程的异步操作。 请参阅下面的“DependencyObject 和线程处理”部分。
- 基本数据绑定和样式支持,方法是使属性设置为表达式,以便在对象的生存期内稍后的某个时间点进行评估。 依赖项 属性概述中更详细地介绍了这些概念。 另请参阅数据绑定。
DependencyObject 和线程
必须在与应用的当前 窗口 关联的 UI 线程上创建所有 DependencyObject 实例。 这由系统强制实施,这对代码有两个重要影响:
- 使用来自两个 DependencyObject 实例的 API 的代码将始终在同一线程上运行,该线程始终是 UI 线程。 在此方案中,通常不会遇到线程问题。
- 主 UI 线程上运行的代码无法直接访问 DependencyObject,因为 DependencyObject 仅与 UI 线程具有线程相关性。 只有 UI 线程上运行的代码才能更改甚至读取依赖属性的值。 例如,使用.NET任务或显式 ThreadPool 线程启动的工作线程将无法读取依赖属性或调用其他 API。
不会完全阻止从工作线程使用 DependencyObject。 但是,必须从 DependencyObject 获取 CoreDispatcher 对象( DependencyObject.Dispatcher 的值),才能跨越应用 UI 线程与系统上运行的任何其他线程之间的故意分离。 CoreDispatcher 公开 RunAsync 方法。 调用 RunAsync 以运行可等待的代码( IAsyncAction)。 如果它是简单的代码,则可以使用 lambda 表达式,否则可以实现为委托(DispatchedHandler)。 系统确定代码可以运行的时间。 由于它在跨线程启用访问, DependencyObject.Dispatcher 是唯一可从非 UI 线程访问的 DependencyObject 或其任何子类的实例 API,无需引发跨线程异常。 如果尝试从工作线程或任何其他非 UI 线程调用它们,所有其他 DependencyObject API 都会引发异常。
通常可以在典型的 UI 代码中避免线程问题。 但是,设备通常不与 UI 线程关联。 如果使用从设备获取的信息实时更新 UI,则通常必须获取 CoreDispatcher ,以便更新 UI。 另一种情况是,用于访问服务的代码可能未在 UI 线程上运行。
如果定义自己的 DependencyObject 类型并尝试将其用于数据源,或者不一定适当(因为对象与 UI 不直接相关),则可能会遇到与 DependencyObject 相关的线程问题的一个代码方案。 例如,你可能尝试对后台线程或其他工作线程进行性能优化,这些线程正在呈现之前更改对象的值,或者响应设备、服务或其他外部输入。 评估是否确实需要方案的依赖项属性;也许标准属性足够。
DependencyObject 派生类
DependencyObject 是多个立即派生类的父类,这些类都是用于应用及其 XAML UI 的编程模型的基础。 下面是一些值得注意的派生类:
- UIElement:大多数可以处理 UI 中输入的视觉对象的基类。 FrameworkElement 在此层次结构中进一步,与 Control 一样,因此Windows 运行时中有数百个控件和其他类,这些控件在其类层次结构中都有 UIElement 类。
- XAML 样式系统支持类:FrameworkTemplate(ControlTemplate 的父级、DataTemplate、ItemsPanelTemplate)、Style、SetterBase(Setter 的父级)、ToolTipTemplateSettings 和其他 *TemplateSettings。
- 视觉状态模型: Transition、 VisualState、 VisualStateGroup、 VisualStateManager、 VisualTransition。
- 数据绑定支持: BindingBase ( Binding 的父级)、 CollectionViewSource、 PropertyPath、 RelativeSource。
- 情节提要动画: 时间线 ( 情节提要的父级、类型化动画、主题动画)、键入的关键帧(如 ColorKeyFrame)、 KeySpline、 EasingFunctionBase。
- XAML 图形和呈现基元: Brush、 Geometry、 GeneralTransform、 GradientStop、 PathFigure、 PathSegment、 投影。
- 图像处理和呈现图面互操作: ImageSource ( BitmapSource 的父级、 RenderTargetBitmap、 SurfaceImageSource 等)。
- 窗口:表示主应用窗口。
- 浮出控件Base ( 浮出控件 和 MenuFlyout 的父级)。
- ResourceDictionary:在 XAML 中定义键控资源。
- TextElement: Block 和 Inline 的父级,而这些元素又是用于 TextBlock 内容或 RTF 元素的文本元素的基类。
- AutomationPeer:表示Microsoft UI 自动化侦听器的 XAML 控件的Microsoft UI 自动化对等方的基类。 此外, IRawElementProviderSimple。
- 杂项:CacheMode、ColumnDefinition、DependencyObjectCollection、InputScope、InputScope、InputScopeName、PageStackEntry、PrintDocument、RowDefinition、TimelineMarker、TriggerAction、TriggerBase。
构造函数
| 名称 | 说明 |
|---|---|
| DependencyObject() |
为 DependencyObject 派生类提供基类初始化行为。 |
属性
| 名称 | 说明 |
|---|---|
| Dispatcher |
获取与此对象关联的 CoreDispatcher 。 CoreDispatcher 表示可以访问 UI 线程上的 DependencyObject 的设施,即使代码是由非 UI 线程启动的。 |
方法
| 名称 | 说明 |
|---|---|
| ClearValue(DependencyProperty) |
清除依赖属性的本地值。 |
| GetAnimationBaseValue(DependencyProperty) |
返回为依赖属性建立的任何基值,在动画未处于活动状态的情况下,该属性将适用。 |
| GetValue(DependencyProperty) |
从 DependencyObject 返回依赖属性的当前有效值。 |
| ReadLocalValue(DependencyProperty) |
如果设置了本地值,则返回依赖属性的本地值。 |
| RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback) |
注册一个通知函数,用于侦听此 DependencyObject 实例上对特定 DependencyProperty 所做的更改。 |
| SetValue(DependencyProperty, Object) |
设置 DependencyObject 上的依赖属性的本地值。 |
| UnregisterPropertyChangedCallback(DependencyProperty, Int64) |
取消以前通过调用 RegisterPropertyChangedCallback 注册的更改通知。 |