本页对应的英文页面已更新,但尚未翻译。 若要查看最新内容,请点击此处访问英文页面。
事件是发生涉及对象的某些更改或操作时的通知广播。侦听程序定义在发生特定事件时执行的函数。类可以定义和触发事件。当代码访问对象属性时,MATLAB® 可以触发预定义的事件。
events | 事件名称 |
notify | 通知侦听程序事件正在发生 |
addlistener | 创建绑定到事件源的事件侦听程序 |
listener | 创建未绑定到事件源的事件侦听程序 |
event.hasListener | 确定事件是否存在侦听程序 |
event.EventData | 事件数据的基类 |
event.ClassInstanceEvent | InstanceCreated 和 InstanceDestroyed 事件的事件数据 |
event.listener | 定义侦听程序对象的类 |
event.PropertyEvent | 属性事件的数据 |
event.proplistener | 定义属性事件的侦听程序对象 |
使用事件和侦听程序发送和响应消息。
事件向所有注册的侦听程序广播通知,并将特定于事件的数据传递给侦听程序回调函数。
这些代码示例说明如何定义事件和侦听程序
Specify a list of classes that can be notified of a specific event.
Control access to events by setting event attributes.
Specify listener callbacks using function handles.
Errors in listener callbacks do not prevent execution of other listeners. However, the order of listener execution is not defined.
MATLAB manages the lifecycle of listeners. For greater control of listener lifecycle, constructor listener objects using the listener
method instead of addlistener
.
Determine If Event Has Listeners
You can determine if there are listeners defined for a specific event and event source.
MATLAB 为所有属性定义了在执行 set 和 get 前后触发的事件。
Assignment When Property Value Is Unchanged
Prevent the triggering of property events when values do not change using the property AbortSet
attribute.
Redefine the event data that MATLAB passes to listeners when an event is triggered.
Observe Changes to Property Values
Listeners can respond to changes in the value of properties using predefined property events.
Implement Property Set Listener
Listeners can respond to changes in property values before or after MATLAB assigns the value.
Techniques for Using Events and Listeners
This example shows how to enable and disable listener execution using context menus.
You can restore property listeners from the loadobj
method.