属性
所有元素都有属性。 内置元素自带诸如颜色或尺寸之类的公共属性。 你可以给它们赋值或赋整个表达式:
export component Example inherits Window { // Simple expression: ends with a semi colon width: 42px; // or a code block (no semicolon needed) height: { 42px }}slint
当你赋一个表达式时,它就成为一个绑定: 每当其所依赖的某个属性发生变化时,Slint 都会自动重新求值。 这正是让用户界面具有响应式的原因—— 你只需声明一次关系,值便会保持最新。
绑定也可以是包含多条语句的代码块。 这两个绑定是等价的:
background: touch-area.is-pressed ? red : blue;
background: { if (touch-area.is-pressed) { return red; } else { return blue; }}slint
声明你自己的属性
Section titled “声明你自己的属性”通过指定类型、名字以及可选的默认值,在组件上定义额外的属性:
export component Example { // declare a property of type int with the name `my-property` property<int> my-property;
// declare a property with a default value property<int> my-second-property: 42;}slint
使用限定符来声明谁可以读写该属性。 把它看作组件的接口:
export component Button { // This is meant to be set by the user of the component. in property <string> text; // This property is meant to be read by the user of the component. out property <bool> pressed; // This property is meant to both be changed by the user and the component itself. in-out property <bool> checked;
// This property is internal to this component. private property <bool> has-mouse;}slint
在组件顶层声明的、所有非 private 的属性,
在把该组件用作元素时都可以从外部访问,
也可以通过语言绑定从业务逻辑访问。
© 2026 SixtyFPS GmbH