2013-10-18 46 views
1

我正在嘗試創建 自定義設計器或 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 

的解決方案的構建和運行沒有問題,但我在設置設計師中完全沒有看到任何東西,因爲我將這個類添加爲類型以供設置使用。

最令人沮喪的是,我不知道轉換器是否工作。

任何指導,將不勝感激。

+0

運動! 我發現我的問題的一部分是我應用於我的課程的屬性。這是錯誤的:' _ _'。通過刪除' _',我可以讓類型轉換器開始工作。 – Seth

回答

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中。

我知道這篇文章很可笑,但希望有人能夠通過它並使用它爲自己的目的。

相關問題