我有一个AppSwitch,它在我的应用程序中被用来“激活/停用”用户帐户,它的检查状态是从一个数据库中读取的,该数据库按预期工作,根据每个用户的bool改变开关和文本。
我的问题是
每次我打开带有AppSwitch的页面时,它都会发送onCheckedChanged信号--然后向用户发送通知--他们的帐户已经激活了?我曾计划通过推送通知通知用户他们的帐户是活动的,但不希望每次管理员移动到他们的页面时都这样发送!
我使用Felgo(以前的V-Play)进行开发,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信号是正常行为吗?或者,我如何修改它,使它只在用户更改它时才会触发!?
谢谢你的帮助!
发布于 2019-07-01 17:12:33
添加一个条件,以确保在组件完整插入后(包括在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
}
}
}https://stackoverflow.com/questions/56817460
复制相似问题