46 lines
2.1 KiB
Plaintext
46 lines
2.1 KiB
Plaintext
// 源类继承原生视觉类型:原生载荷负责基础绘制,新增字段和覆盖方法由源类提供。
|
|
// label 在树中出现多次但仍是同一对象,原生方法和源字段写入共同更新各出现位置。
|
|
// 扩展 TextBlock 的业务字段;其文本/颜色仍由原生父类存储与绘制。
|
|
class StatusText: TextBlock {
|
|
activeColor: String
|
|
|
|
// 用继承的 setText/setColor 改原对象;activeColor 是构造时追加的源字段。
|
|
function activate(self: StatusText, event: Event): Unit {
|
|
self.setText("继承的原生文本已更新")
|
|
self.setColor(self.activeColor)
|
|
}
|
|
}
|
|
|
|
// 继续继承源子类,添加 message 并覆盖事件入口,保留 TextBlock 的原生能力。
|
|
class CustomStatus: StatusText {
|
|
message: String
|
|
|
|
// 显式调用基类 setText 以示范原生未绑定方法的接收者传递,再用继承方法改颜色。
|
|
function activate(self: CustomStatus, event: Event): Unit {
|
|
TextBlock.setText(self, self.message)
|
|
self.setColor(self.activeColor)
|
|
}
|
|
}
|
|
|
|
// 按钮保存目标 Block,事件到来时把同一 Event 转交给目标的动态 activate。
|
|
class ActionButton: Button {
|
|
target: Block
|
|
|
|
// 不复制目标或创建新展示对象,直接调用目标业务方法,选择实际类型的覆盖实现。
|
|
function activate(self: ActionButton, event: Event): Unit {
|
|
self.target.activate(event)
|
|
}
|
|
}
|
|
|
|
// Page 也可增加源字段;caption 用于构建页内标题,不替代原生窗口标题。
|
|
class CustomPage: Page {
|
|
caption: String
|
|
}
|
|
|
|
// 前面位置参数供原生 TextBlock 构造,后面的颜色/消息供继承字段;所有引用都保留托管类型。
|
|
label = CustomStatus("点击按钮更新同一个自定义文本对象", 2, "#242422", "#237A45", "源码子类与原生载荷共同工作")
|
|
button = ActionButton("调用源码子类方法", label)
|
|
button.onClick(button.activate)
|
|
page = CustomPage("Endor 原生类型的源码继承")
|
|
page.render(Column(24, 16, TextBlock(page.caption, 2), label, label, button, ui.textOf(label, "message")))
|