【问题标题】:Is it possible to have a Shared/Static Dependency Property?是否可以拥有共享/静态依赖属性?
【发布时间】:2011-02-23 11:53:01
【问题描述】:

[使用 VB.NET,但我可以轻松阅读响应中的 C# 代码]

我有一个名为QuestionClipboard 的类,具有所有共享方法/属性。

我以前有一个 QuesitonClipboard.doesClipboardHaveContent 函数,如果我的“剪贴板”上有一个对象,它会返回真/假。

我更喜欢实现一个依赖属性,这样我就可以允许这个真/假值参与数据绑定。

“GetValue(dp as DependencyProperty)”方法需要一个对象实例,这意味着我的属性不能被共享!

这是我完美世界中的代码的样子...当然,属性声明之前的“共享”一词会使这段代码无用。

Private Shared clipboardHasContentPropertyKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("clipboardHasContent", GetType(Boolean), GetType(QuestionClipboard), _
                                                                        New PropertyMetadata(False, Nothing, New CoerceValueCallback(AddressOf coerceClipboardHasContent)))
Private Shared clipboardHasContentProperty As DependencyProperty = clipboardHasContentPropertyKey.DependencyProperty

Public SHARED Property clipboardHasContent As Boolean
    Get
        Return GetValue(clipboardHasContentProperty)
    End Get
    Set(ByVal value As Boolean)
        SetValue(value)
    End Set
End Property

【问题讨论】:

  • 你不能使用单例而不是 100% 静态属性/字段?
  • 哇。辛格尔顿。直到现在才遇到它!哈!谢谢,谢谢!

标签: .net wpf vb.net dependencies


【解决方案1】:

让我的评论成为答案。

使用singleton 对象而不是所有静态/共享属性。 Here's an example in C#(VB.NET 离我们不远了),只是让单例类继承自 DependencyObject。

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    相关资源
    最近更新 更多