如何让CF自定义编辑器工作

时间:2010-10-29 11:42:49

标签: compact-framework

我正在为WM6智能设备开发自定义控件库(.net cf 3.5)。我创建了一个名为GradientPanel的自定义控件,猜猜它的作用。一个名为GradientColor的类描述了如何渲染背景。我已经创建了一个自定义typeconverter,以便GradientPanel实例的GradientColor属性可以在Visual Studio属性网格中展开。它工作正常。我还创建了一个自定义编辑器,从UITypeEditor派生出来。我不想要太多,只是为了给出一个视觉线索,看看渐变的样子。代码如下:

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

它确实编译,并且反射部分与自定义类型转换器中使用(并且正在工作)的部分相同。但是,我根本无法让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会采用&lt; TypeConverter&gt;标记到帐户中,但不关心&lt; Editor&gt;标签?或者我错过了什么?

请帮助。

谢谢, TGZ

2 个答案:

答案 0 :(得分:1)

我的不好,System.Drawing.Design.UITypeEditor类不在System.Drawing.Design中,但System.Drawing程序集看起来就是为什么它找不到我的编辑器。好吧,实际上确实找到了它,但它找不到基类。

我测试了它,它看起来像行

e.Graphics.Clear(Color.White)

不需要PaintValue(...)过程的第一行。实际上它清除了属性网格中的所有前面的行,所以应该避免使用它。

答案 1 :(得分:0)

您是如何继承System.ComponentModel.TypeConverter的?提到的TypeConverter只是一个空类。 System.Drawing.Design.UITypeEditor类在紧凑框架上也不可用。

你是怎么做到的?