bot框架v3中的自适应卡是否可以包含动态下拉列表

时间:2019-03-21 05:52:18

标签: c# botframework dropdown chatbot adaptive-cards

我正在使用BOT Framework v 3,我有一个自适应卡,可以从用户那里获取输入,我希望Dropdown中的值是动态的。这是自适应卡设计代码,您可以看到我已经输入了手动选择,而不希望它从数据库中是动态的

var card = new AdaptiveCard()
{
    Body = new List<CardElement>()
    {
        new TextBlock()
        {
            Color = TextColor.Attention,
            Weight = TextWeight.Bolder,
            Size = TextSize.Medium,
            Text = "Select a title",
        },
        new ChoiceSet()
        {
            Id = "title",
            Style = ChoiceInputStyle.Compact,
            IsRequired = false,
            IsMultiSelect = false,
            Value = "1",
            Choices = new List<Choice>()
            {
                new Choice()
                {
                    Title = "Swiss cargo",
                    Value = "Swiss cargo",
                },
                new Choice()
                {
                    Title = "ticket booking",
                    Value = "ticket booking",
                },
            },
        },
    },
};

1 个答案:

答案 0 :(得分:0)

假设您可以将数据放入字符串列表中,则可以使用Linq轻松地动态构建自适应卡。如果要继续使用相同的Adaptive Cards库,则如下所示:

var data = new List<string> { "Swiss cargo", "ticket booking" };

var card = new AdaptiveCard()
{
    Body = new List<CardElement>()
    {
        new TextBlock()
        {
            Color = TextColor.Attention,
            Weight = TextWeight.Bolder,
            Size = TextSize.Medium,
            Text = "Select a title",
        },
        new ChoiceSet()
        {
            Id = "title",
            Style = ChoiceInputStyle.Compact,
            IsRequired = false,
            IsMultiSelect = false,
            Value = "1",
            Choices = data.Select(item => new Choice { Title = item, Value = item }).ToList(),
        },
    },
};