与应用程序设置一起使用的设计时编辑器

时间:2013-10-18 18:46:39

标签: .net settings

我正在尝试为我创建的自定义类型创建自定义设计器或 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

该解决方案可以毫无问题地构建和运行,但是当我将此类添加为用于设置的类型时,我在Settings设计器中看不到任何内容。

最令人沮丧的是,我不知道转换器是否正常工作。

任何指导都将不胜感激。

1 个答案:

答案 0 :(得分:0)

改变课堂装饰后,正如我在评论中提到的那样,我能够让类型转换器正常工作。有了这个,我就可以在UITypeEditors上使用MSDN article来构建我所追求的对话框。

这是代码(实际表单在其他地方处理):

Public Class FileSelectionEditor
Inherits System.Drawing.Design.UITypeEditor

Public Sub New()

End Sub

Public Overloads Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle

    Return UITypeEditorEditStyle.Modal

End Function

Public Overloads Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object

    Dim edSvc As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
    If edSvc Is Nothing Then
        Return Nothing
    End If

    If IsNothing(value) Then
        Dim file As New BATCORE.Configuration.FileSelection
        value = file
    End If

    Using form As New FileSelectionDialog(value)

        If edSvc.ShowDialog(form) = DialogResult.OK Then
            Return form.FileData
        End If

    End Using

    'If OK was not pressed, return the original value
    '
    Return value 
End Function

End Class

最后,在使用设计时代码时,有几件事让我感到高兴:

  • Visual Studio缓存设计时代码。为了对类型转换器或UITypeEditor进行更改,我发现我必须构建项目然后关闭并重新打开解决方案。
  • Alt + Enter是打开项目属性的快捷方式。使用它。
  • 如果要在设置设计器中使用自定义类型,则必须将这些类型存储在自己的项目/ DLL中。

我知道这篇文章的篇幅可笑很长,但希望有人能够通过它并将其用于自己的目的。