事件
定义和触发事件;创建响应事件的侦听程序
事件是发生涉及对象的某些更改或操作时的通知广播。侦听程序定义在发生特定事件时执行的函数。类可以定义和触发事件。当代码访问对象属性时,MATLAB® 可以触发预定义的事件。
函数
events | 事件名称 |
notify | 通知侦听程序事件正在发生 |
addlistener | 创建绑定到事件源的事件侦听程序 |
listener | 创建未绑定到事件源的事件侦听程序 |
event.hasListener | 确定事件是否存在侦听程序 |
类
event.EventData | 事件数据的基类 |
event.ClassInstanceEvent | InstanceCreated 和 InstanceDestroyed 事件的事件数据 |
event.listener | 定义侦听程序对象的类 |
event.PropertyEvent | 属性事件的数据 |
event.proplistener | 定义属性事件的侦听程序对象 |
主题
事件和侦听程序
- 事件和侦听程序概述
使用事件和侦听程序发送和响应消息。 - 事件和侦听程序概念
事件向所有注册的侦听程序广播通知,并将特定于事件的数据传递给侦听程序回调函数。 - 事件和侦听程序语法
这些代码示例说明如何定义事件和侦听程序。 - Event Access List
Specify a list of classes that can be notified of a specific event. - Event Attributes
Control access to events by setting event attributes.
实现侦听程序
- Listener Callback Syntax
Specify listener callbacks using function handles. - Callback Execution
Errors in listener callbacks do not prevent execution of other listeners. However, the order of listener execution is not defined. - Listener Lifecycle
MATLAB manages the lifecycle of listeners. For greater control of listener lifecycle, constructor listener objects using thelistener
method instead ofaddlistener
. - 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 propertyAbortSet
attribute.
事件和侦听程序的示例
- Define Custom Event Data
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. - Restore Listeners
You can restore property listeners from theloadobj
method.