2012-10-03 21 views
2

我正在開發一個自定義用戶控件。用戶控件具有映射到枚舉的屬性,並且不應該有任何默認值,即控件的使用者必須設置它。可以枚舉的枚舉值不會在智能感知或設計視圖中顯示

屬性:

<Description("This is the property description"), 
Category("SomeCategory"), Bindable(True)> 
Public Property SomeProperty As Enumerations.SomeEnumeration? 

枚舉:

Namespace Enumerations 
    Public Enum SomeEnumeration 
     Zero = 0 
     One 
     Two 
    End Enum 
End Namespace 

的檢查:

If SomeProperty Is Nothing Then 
    Throw New ApplicationException("You must set SomeProperty.") 
End If 

問題:

所有的邏輯工作。我的問題是,當您嘗試從標記設置SomeProperty時,沒有任何枚舉值在智能感知中顯示。我的一位同事發現this related support request,所以它似乎是一個已知的問題。

我的問題是,支持所有我需要在此控件上的行爲以及保持此屬性上的智能感知的最佳方式是什麼?

回答

5

我可以重新創建此問題 - 使枚舉爲空可以使智能感知停止工作。我想這是因爲可空類型是對象。

建議保持枚舉爲不可爲空。有一個默認值NotSetNone。如果未設置枚舉,則可以在您的getter或初始化代碼中引發異常。

物業

<Description("This is the property description"), 
Category("SomeCategory"), Bindable(True)> 
Public Property SomeProperty As Enumerations.SomeEnumeration 

枚舉

Namespace Enumerations 
    Public Enum SomeEnumeration 
     NotSet = -1 
     Zero = 0 
     One 
     Two 
    End Enum 
End Namespace 

檢查

枚舉
If SomeProperty Is SomeProperty.NotSet Then 
    Throw New ApplicationException("You must set SomeProperty.") 
End If 
+0

這很好用。謝謝 – jbabey

1
Public Enum SomeEnumeration 
    NotSet = -1 
    Zero = 0 
    One 
    Two 
End Enum 

默認值是0,所以如果你聲明SomeEnumeration的變量,那defaul值變量將是Zer O操作。 例如; SomeEnumeration SomeProperty; SomeEnumeration SomeProperty;

SomeProperty的值將會是SomeEnumeration.Zero