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 10 (introduced in 10.0.10240.0)
Windows.Foundation.UniversalApiContract (introduced in v1.0)
This example defines a class that derives from DependencyObject, and defines an attached property along with the identifier field. The scenario for this class is that it is a service class that declares an attached property that other UI elements can set in XAML The service potentially acts on the attached property values on those UI elements at run time.
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
This example shows a simple dependency property declaration. A call to GetValue constitutes the entirety of the get accessor implementation for the property wrapper of the new dependency property. A call to SetValue constitutes the entirety of the set accessor implementation. For more examples, see Custom dependency properties.
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
The DependencyObject class enables dependency property system services on its many derived classes. For more info about the dependency property concept, see Dependency properties overview.
The dependency property system's primary function is to compute the values of properties, and to provide system notification about values that have changed. Another key class that participates in the dependency property system is DependencyProperty. DependencyProperty enables the registration of dependency properties into the property system, whereas DependencyObject as a base class enables objects to use and set the dependency properties.
Here are some notable services and characteristics that DependencyObject provides or supports:
All DependencyObject instances must be created on the UI thread that is associated with the current Window for an app. This is enforced by the system, and there are two important implications of this for your code:
You aren't completely blocked from using a DependencyObject from a worker thread. But you must get a CoreDispatcher object (the value of DependencyObject.Dispatcher) from a DependencyObject in order to get across the deliberate separation between the app UI thread and any other threads running on the system. The CoreDispatcher exposes the RunAsync method. Call RunAsync to run your awaitable code (an IAsyncAction). If it's simple code you can use a lambda expression, otherwise you can implement as a delegate (DispatchedHandler). The system determines a time that your code can be run. Because it's enabling access across threads, DependencyObject.Dispatcher is the only instance API of DependencyObject or any of its subclasses that can be accessed from a non-UI thread without throwing a cross-thread exception. All other DependencyObject APIs throw an exception if you attempt to call them from a worker thread or any other non-UI thread.
Threading issues can usually be avoided in typical UI code. However, devices aren't usually associated with the UI thread. If you are using info obtained from a device to update the UI in real-time, you often must get a CoreDispatcher so that you can update the UI. Services are another case where the code you use to access the service might not be running on the UI thread.
One code scenario where you might encounter DependencyObject-related threading issues if you are defining your own DependencyObject types and you attempt to use them for data sources, or other scenarios where a DependencyObject isn't necessarily appropriate (because the object is not directly related to the UI). For example, you might be attempting perf optimizations with background threads or other worker threads that are changing values of the objects prior to presentation, or in response to a device, service or other external input. Evaluate whether you really need dependency properties for your scenario; maybe standard properties are adequate.
DependencyObject derived classesDependencyObject is the parent class for several immediately derived classes that are all fundamental to the programming model you use for your app and its XAML UI. Here are some of the notable derived classes:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4