求助:PowerShell中Update-BTNotification无法更新Burnt Toast通知
Burnt Toast通知无法更新进度的问题解决方法
你的代码核心问题是没有使用Burnt Toast的数据绑定占位符语法,而是直接传入了静态值,导致Update-BTNotification无法识别需要更新的字段。
修正后的代码
$image = New-BTImage -Source "D:\Pictures\Profile image.png" -Crop Circle $binding = @{ text1 = "Downloading your files..." text2 = "Your files are being downloaded so they can be available offline." value = 0 } # 进度条的Value使用数据绑定占位符,而非静态值 $progress = New-BTProgressBar -Status "Downloading..." -Value "{binding value}" -Title "Download" -Verbose # 通知文本使用数据绑定占位符,而非直接传静态文本 New-BurntToastNotification -Text "{binding text1}", "{binding text2}" -AppLogo $image -ProgressBar $progress -UniqueIdentifier "toast" -Verbose foreach ($n in 1..100){ $binding['value'] = [math]::Round($binding['value'] + 0.01, 2) # 避免浮点精度问题 $binding['text1'] = "Download progress: $($binding['value']*100)%" Update-BTNotification -UniqueIdentifier "toast" -DataBinding $binding -Verbose Start-Sleep 1 }
关键修改点
- 进度条绑定:创建
New-BTProgressBar时,-Value参数必须设置为{binding value},这样Burnt Toast才知道这个值是动态可更新的。你之前直接传$binding['value'],相当于把初始的0硬编码进了进度条,后续更新无法生效。 - 通知文本绑定:
New-BurntToastNotification的-Text参数改用{binding text1}和{binding text2}占位符,这样文本内容也能随$binding的更新而变化。 - 浮点精度处理:用
[math]::Round处理进度值,避免多次累加后出现的小数精度问题(比如0.01累加100次可能不等于1)。
运行修正后的代码,通知的进度条和文本会每秒正常更新。
内容的提问来源于stack exchange,提问作者Silloky
相关产品推荐
相关产品推荐

