WinUI3/WASDK问题:无法在CarouselPage中添加Input.Toggle控件
Windows 11小部件Carousel中添加复选框的解决方案
你遇到的错误是因为Windows 11小部件的Carousel组件(微软对Adaptive Cards的私有扩展)不允许在CarouselPage的items中直接放置Input类控件。Input.Toggle这类表单控件仅支持在支持表单交互的顶层容器中使用,CarouselPage仅兼容静态展示类控件(如TextBlock、Image、Container等)。
下面是可行的替代方案——用视觉元素模拟复选框,并通过交互动作实现状态切换:
示例代码
"type": "Carousel", "height": "stretch", "rtl": false, "pages": [ { "type": "CarouselPage", "id": "firstPage", "items": [ { "type": "Container", "items": [ { "type": "ColumnSet", "columns": [ { "type": "Column", "width": "auto", "items": [ { "type": "Image", "id": "toggle1Icon", "url": "https://your-domain/unchecked-icon.png", // 替换为未勾选图标地址 "altText": "未勾选", "size": "small" }, { "type": "Image", "id": "toggle1CheckedIcon", "url": "https://your-domain/checked-icon.png", // 替换为勾选图标地址 "altText": "已勾选", "size": "small", "isVisible": false } ] }, { "type": "Column", "width": "stretch", "items": [ { "type": "TextBlock", "text": "Title 1", "wrap": true, "size": "medium" } ] } ], "selectAction": { "type": "Action.ToggleVisibility", "targetElements": [ { "elementId": "toggle1Icon", "visibility": "toggle" }, { "elementId": "toggle1CheckedIcon", "visibility": "toggle" } ], "actions": [ { "type": "Action.Execute", "data": { "toggleId": "toggle1", "action": "toggle" } } ] } } ], "rtl": false } ] }, { "type": "CarouselPage", "id": "secondPage", "items": [ { "type": "Container", "items": [ { "type": "ColumnSet", "columns": [ { "type": "Column", "width": "auto", "items": [ { "type": "Image", "id": "toggle2Icon", "url": "https://your-domain/unchecked-icon.png", "altText": "未勾选", "size": "small" }, { "type": "Image", "id": "toggle2CheckedIcon", "url": "https://your-domain/checked-icon.png", "altText": "已勾选", "size": "small", "isVisible": false } ] }, { "type": "Column", "width": "stretch", "items": [ { "type": "TextBlock", "text": "Title 2", "wrap": true, "size": "medium" } ] } ], "selectAction": { "type": "Action.ToggleVisibility", "targetElements": [ { "elementId": "toggle2Icon", "visibility": "toggle" }, { "elementId": "toggle2CheckedIcon", "visibility": "toggle" } ], "actions": [ { "type": "Action.Execute", "data": { "toggleId": "toggle2", "action": "toggle" } } ] } } ], "rtl": false } ] } ]
方案说明
- 视觉模拟:用两张图标(勾选/未勾选)模拟复选框状态,通过
isVisible属性控制显示/隐藏 - 交互逻辑:点击容器时,通过
Action.ToggleVisibility切换图标展示状态,同时用Action.Execute将状态变更同步到你的业务逻辑(如本地存储或后端接口) - 兼容性:该方案完全符合CarouselPage的控件支持规则,不会触发解析错误
内容的提问来源于stack exchange,提问作者bex
相关产品推荐
相关产品推荐

