You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否切换AdaptiveCard中Action的可见性?现有方案失效

能否切换Adaptive Card中Action的可见性?

Adaptive Card的Action元素无法直接通过Action.ToggleVisibility切换可见性,因为Action.ToggleVisibility的targetElements仅支持定位到卡片body区域内的元素,不能直接作用于actions数组中的Action项,这就是你当前方案失效的原因。

可行的实现方案

要实现类似的按钮切换效果,可以将需要控制的Action放入body内的Container组件中,通过切换Container的可见性间接控制Action的显示/隐藏。以下是修改后的示例代码:

{
  "type": "AdaptiveCard",
  "version": "1.2",
  "body": [
    {
      "type": "TextBlock",
      "text": "Press the buttons to toggle the button!",
      "wrap": true
    },
    {
      "id": "showBtnContainer",
      "type": "Container",
      "actions": [
        {
          "id": "btnShow",
          "type": "Action.ToggleVisibility",
          "title": "Show!",
          "targetElements": [
            {
              "elementId": "showBtnContainer",
              "isVisible": false
            },
            {
              "elementId": "hideBtnContainer",
              "isVisible": true
            }
          ]
        }
      ]
    },
    {
      "id": "hideBtnContainer",
      "type": "Container",
      "isVisible": false,
      "actions": [
        {
          "id": "btnHide",
          "type": "Action.ToggleVisibility",
          "title": "Hide!",
          "targetElements": [
            {
              "elementId": "hideBtnContainer",
              "isVisible": false
            },
            {
              "elementId": "showBtnContainer",
              "isVisible": true
            }
          ]
        }
      ]
    }
  ]
}

方案说明

  • 将原来的两个Action分别放入showBtnContainer和hideBtnContainer两个Container中,这两个Container属于body区域的元素,支持被Action.ToggleVisibility定位。
  • 初始状态下hideBtnContainer设置为不可见,点击“Show!”按钮时隐藏自身容器、显示“Hide!”按钮所在的容器,反之亦然,实现按钮的切换显示效果。

内容的提问来源于stack exchange,提问作者user8555937

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 21:15:41