我正在嘗試創建
自定義設計器或
UITypeEditor併爲我創建的自定義類型輸入轉換器。我希望能夠在「應用程序設置」窗口中使用此編輯器,以便我和我的團隊可以定義此類型以用於我們創建的設置。與應用程序設置兼容的設計時編輯器
這一點我已經成功地創建了序列化爲XML的類,但是我沒有嘗試過實現設計器或類型轉換器,這些嘗試都起作用了,並且大部分演練和示例都已經過了能夠在主題上找到討論自定義表單元素,這不是我所追求的。
理想情況下,我想要的是類似於將字體添加到應用程序設置時遇到的情況。編輯設置值時,對話框允許開發人員輕鬆定義類型的屬性,然後寫入.config文件。這是理想的,但在這一點上,我已經決定了一個實際產生標準值的類型轉換器。如上所述,我可以將這些值輸出到XML中,但我無法弄清楚如何輕鬆定義這些值。我應該採取什麼方法來與設置設計師合作?
編輯
這就是我現在所處的位置。我有類作爲OpenFileDialog的代理定義。這個問題的原因超出了這個問題的範圍,但很快,它與我以編程方式控制的UI元素有關。
這裏的類:
Public Class FileSelection
Private _selectedFile As String
Public Property SelectedFile() As String
Get
Return _selectedFile
End Get
Set(ByVal value As String)
_selectedFile = value
End Set
End Property
Private _initialDir As String
Public Property InitialDirectory() As String
Get
Return _initialDir
End Get
Set(ByVal value As String)
_initialDir = value
End Set
End Property
Private _title As String
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
Private _filter As String
Public Property Filter() As String
Get
Return _filter
End Get
Set(ByVal value As String)
_filter = value
End Set
End Property
End Class
類,本身是做什麼它應該。但是在設計時編輯這個類的屬性是很痛苦的。
據我已經得到了作爲裝飾類,像這樣: <SettingsSerializeAs(SettingsSerializeAs.Xml)> _ <TypeConverter(GetType(FileSelectionConverter))> _
藉助內置的類型轉換,爲盡我所能,由微軟提供的規格:
Public Class FileSelectionConverter
Inherits TypeConverter
Public Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
If TypeOf value Is String Then
Dim v As String() = CStr(value).Split(",")
Dim file As New FileSelection
file.SelectedFile = Convert.ToString(v(0))
file.InitialDirectory = Convert.ToString(v(1))
file.Filter = Convert.ToString(v(2))
file.Title = Convert.ToString(v(3))
Return file
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
If destinationType Is GetType(String) Then
Dim file As FileSelection
file = CType(value, FileSelection)
Return String.Format("{0},{1},{2},{3}", file.SelectedFile, file.InitialDirectory, file.Filter, file.Title)
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
Public Overrides Function GetStandardValuesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function GetStandardValues(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.ComponentModel.TypeConverter.StandardValuesCollection
Dim values As New List(Of String)
Dim file As New FileSelection
file.SelectedFile = "<Selected File>"
file.InitialDirectory = "<Initial Directory>"
file.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
file.Title = "<Dialog Title>"
values.Add(String.Format("{0},{1},{2},{3}", file.SelectedFile, file.InitialDirectory, file.Filter, file.Title))
Dim svc As New StandardValuesCollection(values)
Return svc
End Function
Public Overrides Function IsValid(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal value As Object) As Boolean
Return True
End Function
End Class
的解決方案的構建和運行沒有問題,但我在設置設計師中完全沒有看到任何東西,因爲我將這個類添加爲類型以供設置使用。
最令人沮喪的是,我不知道轉換器是否工作。
任何指導,將不勝感激。
運動! 我發現我的問題的一部分是我應用於我的課程的屬性。這是錯誤的:' _ _'。通過刪除' _',我可以讓類型轉換器開始工作。 –
Seth