See intro blogpost here.
UPDATE Feb. 29, 2012: As hinted at below that this would happen, this blogpost on dependency properties is now outdated. Since the Consumer Preview of Windows 8 released, dependency properties now work exactly like they do in Silverlight and WPF.
---
Declaration of dependency properties has changed in the WinRT. This is a temporary change and will go away when WinRT hits beta, but still good to know if you start prototyping on WinRT today. If you are a control developer this is probably one of the things you would have to change in the most places.
Here’s what the API looks like for Silverlight and WPF for registering a Dependency Property or an Attached Property:
public static DependencyProperty Register(
string name,
Type propertyType,
Type ownerType,
PropertyMetadata typeMetadata);
public static DependencyProperty RegisterAttached(
string name,
Type propertyType,
Type ownerType,
PropertyMetadata defaultMetadata);
And here’s what this currently looks like in WinRT CTP :
public static DependencyProperty Register(
string name,
string propertyTypeName,
string ownerTypeName,
PropertyMetadata typeMetadata);
public static DependencyProperty RegisterAttached(
string name,
string propertyTypeName,
string ownerTypeName,
PropertyMetadata defaultMetadata);
Notice how the PropertyType and PropertyOwnerType is now strings instead of types!
This means you would have to write your code like this to make it cross-compile:
public double MyDoubleProperty
{
get { return (double)GetValue(MyDoublePropertyProperty); }
set { SetValue(MyDoublePropertyProperty, value); }
}
public static readonly DependencyProperty MyDoublePropertyProperty =
DependencyProperty.Register("MyDoubleProperty",
#if NETFX_CORE
"Double", "MyNamespace.MyControl",
#else
typeof(double), typeof(MyNamespace.MyControl),
#endif
new PropertyMetadata(0d));
Also note that you don’t use the full type name for the system types. Ie. here you use “Double” and not “double” or “System.Double”.
You could create a few static methods to replace the Register/RegisterAttached methods that will make your code cross-platform, and switch it out just there when this changes. Here’s one example how this could be accomplished (the ‘ToRTString’ method isn’t fully tested though…):
public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata metadata)
{
return DependencyProperty.RegisterAttached(name,
#if NETFX_CORE
propertyType.ToRTString(), ownerType.ToRTString()
#else
propertyType, ownerType
#endif
, metadata);
}
public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata metadata)
{
return DependencyProperty.Register(name,
#if NETFX_CORE
propertyType.ToRTString(), ownerType.ToRTString()
#else
propertyType, ownerType
#endif
, metadata);
}
#if NETFX_CORE
private static string ToRTString(this Type type)
{
string name = type.FullName;
if(name.StartsWith("System."))
return name.Substring(7);
else
return name;
}
#endif