2010-10-29 15 views
1

我正在爲WM6智能設備(.net cf 3.5)定製控制庫。我創建了一個名爲GradientPanel的自定義控件,猜測它的功能。一個名爲GradientColor的類描述瞭如何呈現背景。我已經創建了一個自定義的類型轉換器,以便GradientPanel實例的GradientColor屬性在Visual Studio屬性網格中可擴展。它工作正常。我還創建了一個自定義編輯器,從UITypeEditor派生出來。我不想太多,只是爲了給出一個視覺線索如何漸變看起來像。代碼如下:如何讓CF定製編輯器工作

Imports System.Drawing 
Imports System.Drawing.Drawing2D 

Namespace Drawing.Design 

Public Class GradientColorEditor 
    Inherits System.Drawing.Design.UITypeEditor 

    Public Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.Drawing.Design.UITypeEditorEditStyle 
     Return System.Drawing.Design.UITypeEditorEditStyle.None 
    End Function 

    Public Overrides Function GetPaintValueSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean 
     Return True 
    End Function 

    Public Overrides Sub PaintValue(ByVal e As System.Drawing.Design.PaintValueEventArgs) 
     e.Graphics.Clear(Color.White) 

     Dim value = e.Value 
     Dim valType As Type = value.GetType() 
     Dim startColorInfo As System.Reflection.PropertyInfo = valType.GetProperty("StartColor") 
     Dim startColor As System.Drawing.Color = startColorInfo.GetValue(value, Nothing) 
     Dim endColorInfo As System.Reflection.PropertyInfo = valType.GetProperty("EndColor") 
     Dim endColor As System.Drawing.Color = endColorInfo.GetValue(value, Nothing) 
     Dim fillDirectionInfo As System.Reflection.PropertyInfo = valType.GetProperty("FillDirection") 
     Dim fillDirType As Type = fillDirectionInfo.PropertyType 
     Dim fillDirNames() As String = [Enum].GetNames(fillDirType) 
     Dim fillDirValues() As Integer = [Enum].GetValues(fillDirType) 
     Dim fillDirValNameDict As New System.Collections.Generic.Dictionary(Of Integer, String)() 

     For cnt As Integer = 0 To fillDirValues.Length - 1 
      fillDirValNameDict.Add(fillDirValues(cnt), fillDirNames(cnt)) 
     Next 

     Dim fillDir As Integer = fillDirectionInfo.GetValue(value, Nothing) 
     If startColor = System.Drawing.Color.Transparent OrElse endColor = System.Drawing.Color.Transparent Then 
      Exit Sub 
     End If 

     Dim brush As New LinearGradientBrush(e.Bounds, startColor, endColor, _ 
              If(fillDir = 0, LinearGradientMode.Horizontal, LinearGradientMode.Vertical)) 

     e.Graphics.FillRectangle(brush, e.Bounds) 
    End Sub 

End Class 

End Namespace 

它編譯和反射部分是相同的,在自定義typeconverter中使用(和正在工作)。但是我根本無法讓Visual Studio使用這個編輯器,這真的讓我發瘋。 這裏是XMTA文件的相關部分:

<Class Name="Tgz.Controls.GradientPanel"> 
    <DesktopCompatible>true</DesktopCompatible> 
    <Property Name="GradientColor"> 
    <TypeConverter> 
    Tgz.Drawing.Design.GradientColorConverter, Tgz.Drawing.Design, 
    Version=0.1.0.0, Culture=neutral, PublicKeyToken=3f315c03f85ce5c1 
    </TypeConverter> 
    <Browsable>true</Browsable> 
    <Category>Appearance</Category> 
    <Description>Defines the gradient background of the control.</Description> 
    <EditorBrowsable>true</EditorBrowsable> 
    <Editor> 
    <Type> 
    Tgz.Drawing.Design.GradientColorEditor, Tgz.Drawing.Design, 
    Version=0.1.0.0, Culture=neutral, PublicKeyToken=3f315c03f85ce5c1 
    </Type> 
    <BaseType> 
    System.Drawing.Design.UITypeEditor, System.Drawing.Design, 
    Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
    </BaseType> 
    </Editor> 
    </Property> 
</Class> 

那麼怎麼來的Visual Studio採取<的TypeConverter >標籤考慮,但不關心<編輯>標籤?或者我錯過了什麼?

請幫忙。

感謝, TGZ

回答

1

我的壞,該System.Drawing.Design.UITypeEditor類不在System.Drawing.Design,但System.Drawing中裝配,貌似這就是爲什麼它沒有找到我編輯。那麼,它確實找到了它,但它沒有找到基類。

我測試了它,雖然,它看起來像行

e.Graphics.Clear(Color.White) 

這是PaintValue(...)程序的第一行是沒有必要的。事實上,它清除了屬性網格中的所有前面的行,所以應該避免。

0

你是如何從System.ComponentModel.TypeConverter繼承的?提到的TypeConverter只是一個空的類。緊湊框架上也沒有類System.Drawing.Design.UITypeEditor。

你是怎麼做到的?