DependencyProperty 的 DefaultValue 何时通过 PropertyMetadata 定义获得其值?

发布于 2024-10-07 01:45:05 字数 1269 浏览 0 评论 0原文

我有两个具有共同底层基础设施的项目。第一个是 Silverlight 3,第二个是 Silverlight 4。

我有一个 ViewRegionControl,我在 XAML 中声明它来定义区域。 ViewRegionControl 类负责管理哪个注册视图可见等等。它有一个依赖属性,充当注册区域的注册表。

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); }
}

默认值作为从我的 IoC 容器实例化的对象传递。在这两种实现中,ViewRegionRegistry 都不是由 XAML 设置的,只能通过此点设置。

预期的初始化在我的 Silverlight 3 项目中成功进行,但在我的 Silverlight 4 项目中却失败了。 Silverlight 4 发生了什么变化?在 Silverlight 4 中,依赖属性是否会延迟获取其默认值?

这里的目的是创建一个通过 ViewRegionRegistry dp 获得的 Singleton 引用。

检查 http://msdn.microsoft.com/ en-us/library/cc903949%28v=VS.95%29.aspx

虽然,我在另一个 dp 的回调中使用了一个 dp,并且 msdn 明确指出:

“避免这些问题的一种方法是确保回调使用仅其他依赖属性,并且每个此类依赖属性都有一个已建立的默认值作为其注册元数据的一部分。”

我就是这么做的。为什么版本之间的行为不同?

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 ViewRegionRegistry 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2024-10-14 01:45:05

必须在类型发生静态初始化时建立默认值。所以我怀疑问题确实是 Silverlight 3 和 4 之间的静态初始化时间是否发生了变化?

发生的变化是 Silverlight 4 的代码执行基于 CLR 4。在 3.5 和 4 之间,执行类型初始值设定项的时间发生了变化。请参阅这篇文章 .NET 4.0 中的类型初始化更改 (由一个叫乔恩·斯基特的家伙写的)。

因此,您可以尝试的一件事是向您的类型添加一个空的静态构造函数,看看是否有帮助:-

 public static ViewRegionControl() { }

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() { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文