Thursday, May 8, 2008

How To Catch Rayquaza Shiny Easier

AttachedProperties properly initialize

Sometimes it is appropriate to after an object has registered with a property attached, perform some initialization. Here, in the set method by placing the attached property, such as an initialization method will be called.


SetAttachedProperty (

DependencyObject obj, int

value)
{
obj.SetValue (attached property, value);

InitializeProperty ();}



The Set method does nothing more than calling for the passed DependencyObjekt

SetValue

. The actual value of the attached property is set so as a normal DependencyProperty. Just imagine Just imagine a DependencyProperty is nothing but a key is stored with a value in a Dictionary. Each DependencyObject has such a dictionary. The values can also be queried by other objects and set.
To set an attached property, it actually made only the DependencyProperty object on your own to share, not the container object by

set [property]

! This also means that the initialization is no longer called, because the attached property is set on a different path. Now you can make it to the Convention in the course in an API requires that AttachedProperties must be set always set [property]
. So the plan is XamlReader instructed that if he attached a property in the markup recognizes a corresponding set method is available that can call it.

Nevertheless, it is better to perform an initialization different. How? Through a callback mechanism. Each DependencyProperty has metadata that allow code and callbacks for validation, deposit changes, etc.. A change as the value of a DependencyProperty, then the callback is called and initialization can be performed. This mechanism operates independently was set as a DependencyProperty.

public static

ReadOnly


DependencyProperty

property with call back;

/ / Define the metadata callback

PropertyMetadata

metadata =

new

PropertyMetadata ();

metadata.PropertyChangedCallback + = OnPropertyWithCallbackChanged;.

/ / register the DependencyProperty


property with callback =
DependencyProperty

RegisterAttached (

"property with call back"
,


typeof ( int ) , typeof (Window1
), metadata);


/ / The callback calls the real Initializierungsmethode
private


static

void
OnPropertyWithCallbackChanged (

DependencyObject obj,

DependencyPropertyChangedEventArgs value)
{
InitializeProperty ();}


0 comments:

Post a Comment