C#winforms中的属性指定资源选择器?

时间:2012-10-28 22:45:10

标签: c# winforms properties attributes embedded-resource

我在C#中为WinForms做了一个自定义控件。其中一个属性是将在某个时刻播放的音频流。有没有办法告诉VS,应该从具有属性或类似资源的资源清单(特别是音频资源)中选择此属性?

The property, defined.

评论属性是一个音频流。

Control properties.

这就是它在WinForms设计器中的显示方式。

Binary data editor.

这是您单击编辑[...]按钮时获得的。我希望将其替换为[音频]资源选择器。

2 个答案:

答案 0 :(得分:1)

您正在寻找的是:

[Editor(typeof(MyAudioEditor), typeof(UITypeEditor)]

此属性允许您指定在属性网格中显示该属性时要使用的特定编辑器。

然后,您可以从基本类型派生和创建新编辑器。基类型必须或必须派生自System.Drawing.Design.UITypeEditor。

在大多数情况下,当调用编辑器时,您弹出一个您选择的表单,并将其返回值绑定到您的属性。

UITypeEditor有4个虚拟方法和一个虚拟属性,可让您在有人与您的媒体资源互动时或在网格中绘制您的媒体资源时更改所有行为。

答案 1 :(得分:0)

我在下面发布了一个代码,说明我将如何自己做。

只是一些观察:

  • 在我的代码中,我创建了一个String属性AudioName,它将在设计器中设置。此属性引用程序集中的资源名称。然后将从所选资源名称中检索您感兴趣的Stream类型的Audio(只读)属性。
  • 正如Marc-AndréJutras在回答中指出的那样,您需要编写自己的UITypeEditorTypeConverter代码来从程序集中检索音频资源。在这种情况下,我选择实现自定义TypeConverter

    Public Class Form1
        <System.ComponentModel.TypeConverter(GetType(AudioConverter)), System.ComponentModel.DefaultValue("(none)")>
        Public Property AudioName As String
    
        <System.ComponentModel.Browsable(False)> 'This will make the property not appear in the designer
        Public ReadOnly Property Audio As Stream
            Get
                If String.IsNullOrWhiteSpace(AudioName) OrElse
                    AudioName = "(none)" Then
                    Return Nothing
                End If
                Try
                    Return My.Resources.ResourceManager.GetStream(AudioName)
                Catch ex As Exception
                    Return Nothing
                End Try
            End Get
        End Property
    End Class
    
    Public Class AudioConverter
        Inherits System.ComponentModel.TypeConverter
    
        Public Overrides Function GetStandardValuesSupported(context As System.ComponentModel.ITypeDescriptorContext) As Boolean
            Return True
        End Function
    
        ' Set the property to be informed only from predefined (standard) values
        Public Overrides Function GetStandardValuesExclusive(context As System.ComponentModel.ITypeDescriptorContext) As Boolean
            Return True
        End Function
    
        ' Get possible values that will be inserted in the drop-down list
        Public Overrides Function GetStandardValues(context As System.ComponentModel.ITypeDescriptorContext) As System.ComponentModel.TypeConverter.StandardValuesCollection
    
            Dim returnList As New System.Collections.Generic.List(Of String)
            returnList.Add("(none)")
            For Each resItem As System.Collections.DictionaryEntry In My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.InvariantCulture, True, False)
                Dim resourceEntry As String = resItem.Key
                If TypeOf resourceEntry Is String Then
                    returnList.Add(resourceEntry)
                End If
            Next
    
            Return New System.ComponentModel.TypeConverter.StandardValuesCollection(returnList)
        End Function
    End Class