2010-05-25 100 views
1

[使用VB.NET,但我可以輕鬆閱讀C#代碼中的回覆]是否有可能擁有共享/靜態依賴屬性?

我有一個叫做QuestionClipboard的類,帶有ALL共享方法/屬性。

我以前有一個QuesitonClipboard.doesClipboardHaveContent函數,如果我的'剪貼板'上有一個對象,則返回true/false。

我寧願實現一個依賴屬性,所以我可以允許這個真/假值參與數據綁定。

「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 
+1

你不能使用單身而不是100%的靜態屬性/字段? – 2010-05-25 01:47:47

+0

哇。辛格爾頓。直到現在,永遠不要跑過去!哈!謝謝謝謝! – 2010-05-25 01:53:53

回答

3

讓我的評論一個答案。

使用singleton對象而不是所有靜態/共享屬性。 Here's an example in C#(VB.NET不算太遠),並且只有從DependencyObject繼承的單例類。

相關問題