异步操作中不显示设置了显示于应用之上的toast
已解决
1. 自定义了toast,设置了showMode: promptAction.ToastShowMode.TOP_MOST属性,使toast可以显示于应用之上;
2. 在异步操作中,调用toast,无法显示,去除showMode: promptAction.ToastShowMode.TOP_MOST可以正常显示。
// Index.ets
import { showToast } from './ComponentUtil';
import { bundleManager } from '@kit.AbilityKit'
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.getVersionInfo()
})
}
.width('100%')
}
.height('100%')
}
private async getVersionInfo() {
bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
.then((bundleInfo) => {
showToast(bundleInfo.versionName)
}).catch((error: string) => {
console.error("获取版本号异常:" + error)
})
}
}// ComponentUtil.ets
import promptAction from '@ohos.promptAction';
/**
* duration 显示时间3S
* @param msg
*/
export function showToast(msg: string | Resource) {
promptAction.showToast({
message: msg,
duration: 3000,
bottom:"center",
showMode: promptAction.ToastShowMode.TOP_MOST
});
}
class ComponentUtil {
getStringByResourceStr(context: Context, resourceStr: Resource) {
return context.resourceManager.getStringSync(resourceStr).toString()
}
}
let componentUti: ComponentUtil = new ComponentUtil()
export default componentUti as ComponentUtil 1
1
浏览276 发布于2024-09-24 08:46广东
全部评论
最多点赞
最新发布
最早发布
写回答
所见即所得编辑器, editor1
工具栏
0/50000
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
了解社区公约,与您携手共创和谐专业的开发者社区。





































苏公安网备 32011402010933号