DefaultEventAttribute 类
指定组件的默认事件。
**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)
语法
声明
<AttributeUsageAttribute(AttributeTargets.Class)> _
Public NotInheritable Class DefaultEventAttribute
Inherits Attribute
用法
Dim instance As DefaultEventAttribute
[AttributeUsageAttribute(AttributeTargets.Class)]
public sealed class DefaultEventAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class)]
public ref class DefaultEventAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class) */
public final class DefaultEventAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Class)
public final class DefaultEventAttribute extends Attribute
备注
使用 Name 属性来获取默认事件的名称。
有关更多信息,请参见 属性 (Attribute) 概述 和 利用属性扩展元数据。
示例
下面的示例定义名为 MyCollection
的集合类。该类标记为 DefaultEventAttribute,它指定 CollectionChanged
作为默认事件。
<DefaultEvent("CollectionChanged")> _
Public Class MyCollection
Inherits BaseCollection
Public Event CollectionChanged (ByVal sender As Object, _
ByVal e As CollectionChangeEventArgs)
' Insert additional code.
End Class 'MyCollection
[DefaultEvent("CollectionChanged")]
public class MyCollection : BaseCollection {
private CollectionChangeEventHandler onCollectionChanged;
public event CollectionChangeEventHandler CollectionChanged {
add {
onCollectionChanged += value;
}
remove {
onCollectionChanged -= value;
}
}
// Insert additional code.
}
[DefaultEvent("CollectionChanged")]
public ref class TestCollection: public BaseCollection
{
private:
CollectionChangeEventHandler^ onCollectionChanged;
public:
event CollectionChangeEventHandler^ CollectionChanged
{
public:
void add(CollectionChangeEventHandler^ eventHandler)
{
onCollectionChanged += eventHandler;
}
protected:
void remove(CollectionChangeEventHandler^ eventHandler)
{
onCollectionChanged -= eventHandler;
}
}
// Insert additional code.
};
/** @attribute DefaultEvent("CollectionChanged")
*/
public static class MyCollection extends BaseCollection
{
private CollectionChangeEventHandler onCollectionChanged;
public void add_onCollectionChanged(CollectionChangeEventHandler value)
{
onCollectionChanged = (CollectionChangeEventHandler)
System.Delegate.Combine(onCollectionChanged, value);
}
public void remove_onCollectionChanged(CollectionChangeEventHandler
value)
{
onCollectionChanged = (CollectionChangeEventHandler)
System.Delegate.Remove(onCollectionChanged, value);
}
// Insert additional code.
} //MyCollection
下一个示例创建 MyCollection
的实例。然后它获取该类的属性,提取 DefaultEventAttribute,并输出默认事件的名称。
Public Shared Function Main() As Integer
' Creates a new collection.
Dim myNewCollection As New MyCollection()
' Gets the attributes for the collection.
Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(myNewCollection)
' Prints the name of the default event by retrieving the
' DefaultEventAttribute from the AttributeCollection.
Dim myAttribute As DefaultEventAttribute = _
CType(attributes(GetType(DefaultEventAttribute)), DefaultEventAttribute)
Console.WriteLine(("The default event is: " & myAttribute.Name))
Return 0
End Function 'Main
public static int Main() {
// Creates a new collection.
MyCollection myNewCollection = new MyCollection();
// Gets the attributes for the collection.
AttributeCollection attributes = TypeDescriptor.GetAttributes(myNewCollection);
/* Prints the name of the default event by retrieving the
* DefaultEventAttribute from the AttributeCollection. */
DefaultEventAttribute myAttribute =
(DefaultEventAttribute)attributes[typeof(DefaultEventAttribute)];
Console.WriteLine("The default event is: " + myAttribute.Name);
return 0;
}
int main()
{
// Creates a new collection.
DefaultEventAttributeExample::TestCollection^ newCollection =
gcnew DefaultEventAttributeExample::TestCollection;
// Gets the attributes for the collection.
AttributeCollection^ attributes =
TypeDescriptor::GetAttributes(newCollection);
// Prints the name of the default event by retrieving the
// DefaultEventAttribute from the AttributeCollection.
DefaultEventAttribute^ attribute = (DefaultEventAttribute^)
attributes[DefaultEventAttribute::typeid];
Console::WriteLine("The default event is: {0}", attribute->Name);
}
public static void main(String[] args)
{
// Creates a new collection.
MyCollection myNewCollection = new MyCollection();
// Gets the attributes for the collection.
AttributeCollection attributes = TypeDescriptor.
GetAttributes(myNewCollection);
/* Prints the name of the default event by retrieving the
* DefaultEventAttribute from the AttributeCollection. */
DefaultEventAttribute myAttribute = (DefaultEventAttribute)attributes.
get_Item(DefaultEventAttribute.class.ToType());
Console.WriteLine("The default event is: " + myAttribute.get_Name());
} //main
继承层次结构
System.Object
System.Attribute
System.ComponentModel.DefaultEventAttribute
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
请参见
参考
DefaultEventAttribute 成员
System.ComponentModel 命名空间
Attribute