Happy stress free coding

Jul 17, 2013

DependencyProperties that stopped working

DependencyProperties stopped working when moving to Windows Store.

Suppose you have something like this:

 

    public DateTime StopTime
    {
      set { SetValue(GanttUserControl.StopTimeProperty, value); }
      get { return (DateTime)GetValue(GanttUserControl.StopTimeProperty); }
    }
    public static DependencyProperty StopTimeProperty = DependencyProperty.Register("StopTime", typeof(DateTime), 
                typeof(GanttUserControl), new PropertyMetadata(
                   new PropertyChangedCallback(GanttUserControl.StopTimeChanged)));

    private static void StopTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      REALLY IMPORTANT THAT THIS CODE RUNS WHENEVER THE PROPERTY IS CHANGED
    }

And you use it in windows WPF, Silverlight, or on Windows Phone 7 and it works just fine.

Then you want to compile this for use with Windows Store and it stopped working. At least the StopTimeChanged is not called anymore! What can be the cause?

Well it turns out that the available overloads for PropertyMetadata has changed from this:

public PropertyMetadata(PropertyChangedCallback propertyChangedCallback);
public PropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback);

to this:

public PropertyMetadata(object defaultValue);
public PropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback);

This means that the compiler now assumes that your onchange-delegate is the initial value for the property and that you no longer want a onchange-delegate.

Sneaky!

 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home