When does the DefaultValue of a DependencyProperty gains it's value throught the PropertyMetadata definition?
I have two projects with common underlying infrastructure. The first is Silverlight 3 and the second is Silverlight 4.
I have a ViewRegionControl, which I declare in XAML to define regions. ViewRegionControl class is responsible for managing which registered view is visible and more. It has a dependency property which acts as a registry for registered regions.
public static readonly DependencyProperty ViewRegionRegistryProperty = DependencyProperty.Register(
"ViewRegionRegistry",
typeof(IViewRegionRegistry),
typeof(ViewRegionControl),
new PropertyMetadata(IoC.TryResolve<IViewRegionRegistry>()));
public IViewRegionRegistry ViewRegionRegistry
{
get { return (IViewRegionRegistry)GetValue(ViewRegionRegistryProperty); }
set { SetValue(ViewRegionRegistryProperty, value); }
}
The default value is passed as an object instantiated from my IoC container. In both implementations, the ViewRegionRegistr开发者_开发知识库y isn't set by XAML nowhere and only via this spot.
The intended initialization happens successfully at my Silverlight 3 project but not at my Silverlight 4 one. What has changed in Silverlight 4? Does a dependency property gets it's default value lazily in Silverlight 4?
The intention here is to create a Singleton reference obtained through the ViewRegionRegistry dp.
Check http://msdn.microsoft.com/en-us/library/cc903949%28v=VS.95%29.aspx
Although, I used one dp in another dp's callback and msdn says clearly:
"One way to avoid these issues is to make sure that callbacks use only other dependency properties, and that each such dependency property has an established default value as part of its registered metadata."
I did that. Why the behaviour differs between versions?
The default value has to be established at the time that Static Initialization occurs for the type. So I suspect the question really is has the timing of Static Initialization changed between Silverlight 3 and 4?
What has changed is that Silverlight 4 execution of code is based on CLR 4. There have been changes to the when type initializers are executed between 3.5 and 4. See this article Type initialization changes in .NET 4.0 (by some bloke called Jon Skeet).
One thing you therefore might try is to add an empty Static constructor to your type to see if that helps:-
public static ViewRegionControl() { }
精彩评论