首页 > 解决方案 > 当卡片具有超过 5 个操作时,自适应卡片不会在 Spfx 上呈现

问题描述

问题:不渲染超过五个动作

背景:

我正在将 BotFramework-WebChat 与 SharePoint 框架集成,一切正常。但是,当我尝试在 WebChat 中显示具有超过 5 个操作的自适应卡片时,聊天显示给我一张带有“无法呈现卡片”文本的卡片,但如果在 BotFramework-Emulator 中测试可以正常工作或控制台 Directline 可以正常工作。

对于测试建议,我编写了一个 C# 方法,您可以在其中传递要在聊天中显示的操作数。

使用自适应卡进行测试

问题:如果有五个以上的操作,则呈现带有“无法呈现卡片”消息的卡片

异常:渲染一张具有五个以上动作的自适应卡片 private async Task MessageReceivedAsync(IDialogContext context, IAwaitable argument) { var message = await argument; var 回复 = context.MakeMessage();

    if (message.Text.Equals("3 btn"))
    {
        reply.Attachments.Add(GetAdaptativeCard(3));
        reply.Value = " { nombre : 'BTN'}";
    }
    else if (message.Text.Equals("5 btn"))
    {
        reply.Attachments.Add(GetAdaptativeCard(5));
        reply.Value = " { nombre : 'BTN'}";
    } 
    else if (message.Text.Equals("7 btn"))
    {
        reply.Attachments.Add(GetAdaptativeCard(7));
        reply.Value = " { nombre : 'BTN'}";
    }
    else if (message.Text.Equals("18 btn"))
    {
        reply.Attachments.Add(GetAdaptativeCard(18));
        reply.Value = " { nombre : 'BTN'}";
    }
    else
    if (message.Text.Equals("gif"))
    {
        reply.Attachments.Add(GetAnimationCard());
    await context.PostAsync(reply);
}

private Attachment GetAdaptativeCard(int numberOfButtons) {

    var actions = new List<ActionBase>();
    for (int i = 0; i < numberOfButtons; i++)
    {
        actions.Add(new SubmitAction()
        {
            Title = i.ToString(),
            Speak = "<s>Search</s>",
            DataJson = "{ \"Type\": \"HotelSearch\" }"
        });
    }

    AdaptiveCard card = new AdaptiveCard()
    {
        Body = new List<CardElement>()
        {
            new Container()
            {
                Speak = "<s>Hello!</s><s>How many buttons are in the chat?(18)</s>",
                Items = new List<CardElement>()
                {
                    new ColumnSet()
                    {
                        Columns = new List<Column>()
                        {
                            new Column()
                            {
                                Size = ColumnSize.Auto,
                                Items = new List<CardElement>()
                                {
                                    new Image()
                                    {
                                        Url = "https://placeholdit.imgix.net/~text?txtsize=65&txt=Adaptive+Cards&w=300&h=300",
                                        Size = ImageSize.Medium,
                                        Style = ImageStyle.Person
                                    }
                                }
                            },
                            new Column()
                            {
                                Size = ColumnSize.Stretch,
                                Items = new List<CardElement>()
                                {
                                    new TextBlock()
                                    {
                                        Text =  "Hello!",
                                        Weight = TextWeight.Bolder,
                                        IsSubtle = true
                                    }
                                }
                            }
                        }
                    }
                }
        }
    },
        // Buttons
        Actions = actions
    };
    return new Attachment()
    {
        ContentType = AdaptiveCard.ContentType,
        Content = card
    };

}

截图:

使用 HeroCard 进行测试:

我只是尝试使用 HeroCard 而不是 AdaptiveCard。在这种情况下,当超过 5 个按钮时,只显示前 5 个按钮。也许这是一个限制?

问题:卡片仅呈现前五个动作

异常:渲染超过五个动作的英雄卡

private Attachment GetHeroCard(int numbersOfButtons)
{
    var actions = new List<CardAction>();
    for (int i = 0; i < numbersOfButtons; i++)
    {
        actions.Add(new CardAction(ActionTypes.OpenUrl, $"Get Started {i}", value: "https://docs.microsoft.com/bot-framework"));
    }
    HeroCard card = new HeroCard()
    {
        Title = "BotFramework Hero Card",
        Subtitle = "Your bots — wherever your users are talking",
        Text = "Build and connect intelligent bots to interact with your users naturally wherever they are, from text/sms to Skype, Slack, Office 365 mail and other popular services.",
        Images = new List<CardImage> { new CardImage("https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg") },
        Buttons = actions
    };
    return card.ToAttachment();
}

标签: c#botframeworkadaptive-cardsspfx

解决方案


这是网络聊天中自适应卡片的限制。幸运的是,有一个解决方法。

如果不进行任何更改,您将看到 5 个限制,Actions当您转到可视化工具并添加超过 5 个操作时,您可以看到这一点,例如

{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
}

你会得到这个错误:

在此处输入图像描述

但是,您可以更改此设置。它可以通过在可视化工具中编辑主机配置文件来实现, 特别是该maxactions字段。webchat 还包含一个主机配置文件,因此理论上您可以只编辑该文件,它应该可以工作。

"actions": {
    "maxActions": 20,
    "spacing": "default",
    "buttonSpacing": 8,
    "showCard": {
      "actionMode": "inline",
      "inlineTopMargin": 8
    },
    "actionsOrientation": "vertical",
    "actionAlignment": "stretch"
  },

推荐阅读