我有一个 AppSwitch
,它用于在我的应用程序中'激活/停用'用户帐户,从数据库中读取它的检查状态,该数据库是按预期工作的,更改了开关。和文字取决于每个用户的bool。
我的问题是
每次我使用AppSwitch
打开页面时,它都会发送onCheckedChanged
信号 - 依次向用户发送通知他们的帐户已激活吗?我原本计划通知用户他们的帐户在推送通知中处于活动状态,但不希望每次管理员移动到他们的页面!
我正在使用Felgo(以前是V-play(进行开发,AppSwitch
文档的链接是:
Felgo AppSwitch
我的代码是:
Rectangle {
width: parent.width / 2
height: parent.height
Column {
id: column
anchors.fill: parent
AppButton {
id: groupStatusText
width: parent.width
height: parent.height / 2
}
AppSwitch {
id: editStatus
knobColorOn: "#b0ced9"
backgroundColorOn: "#81b1c2"
checked: {
//userListItem is the current profile details of the user page
if(userListItem.groupStatus === 1) {
groupStatusText.text = "Deactivate this user"
}
else if(userListItem.groupStatus === 0) {
groupStatusText.text = "Activate this user"
}
}
onCheckedChanged: {
if(editStatus.checked === true) {
//the signal which sends to my database on updating the value,
//works as intended but sends signal each time page is created?
detailPage.adminEditGroupStatus(1)
} else {
detailPage.adminEditGroupStatus(0)
}
}
}
}
onCheckedChanged
信号在每个实例上发射页面是否加载是正常的行为?或我该如何修改仅当用户更改它时会发射!?
感谢您的任何帮助!
添加条件,以确保完全启动组件后发送信号(包括在checked
属性上创建绑定后(。
您可以在交换机上添加布尔属性:
AppSwitch {
property bool loaded: false
Component.onCompleted: loaded = true
onCheckedChanged: {
if (loaded) {
// notify user
}
}
}
或简单地使用state
:
AppSwitch {
Component.onCompleted: state = "loaded"
onCheckedChanged: {
if (state === "loaded") {
// notify user
}
}
}