简体   繁体   English

自适应卡片中的轮播

[英]Carousel in adaptive card

Please guide me to create carousel adaptive card in MS bot framework.请指导我在 MS bot 框架中创建轮播自适应卡片。 I am using .Net sdk.I tried using adaptive card designer to design but couldn't do it.我正在使用 .Net sdk。我尝试使用自适应卡设计器进行设计,但无法做到。

Your question isn't really specific enough for me to understand where you are having trouble, but I can give you a basic outline of creating a card carousel.你的问题不够具体,我无法理解你在哪里遇到了问题,但我可以给你一个创建卡片轮播的基本大纲。 My code is nodejs but it should be similar enough to give you an idea.我的代码是 nodejs,但它应该足够相似,可以给你一个想法。

You will need CardFactory and MessageFactory to generate first the cards and then the Carousel (which takes an array of cards as the input).您将需要 CardFactory 和 MessageFactory 来首先生成卡片,然后是 Carousel(它将卡片数组作为输入)。

// First create an empty array for your carousel
var cardArray = [];

// Populate the array with your cards (can use any method, I used a for loop)
for (var idx = 0; idx < dataForCards.length; idx++) {
   // Create the adaptive card
   var adaptiveCard = CardFactory.adaptiveCard({

   // YOUR CARD DEFINITION HERE

   });
   // Push the card to the array for the carousel
   cardArray.push(adaptiveCard);
}
// Send the array as a carousel
await step.context.sendActivity(MessageFactory.carousel(cardArray));

Well, Adaptive card designer helps you to create template for one single card.好吧,自适应卡片设计器可帮助您为一张卡片创建模板。 In you case, based on your list create attachment from the created template in a loop and add each of the generated attachments to Activity.Attachments.在您的情况下,根据您的列表从循环中创建的模板创建附件,并将每个生成的附件添加到 Activity.Attachments。

if(listOfReservationCardsData.Any())
        {
            foreach (var checkInStatusCardData in listOfReservationCardsData.OrderBy(l => Convert.ToDateTime(l.StartDate)))
            {
                listOfAttachments.Add(CreateAdaptiveCardAttachment(filePath, data));
            }
        }

        if (listOfAttachments.Any())
        {
            turnContext.Activity.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            turnContext.Activity.Attachments = listOfAttachments.Take(5).ToList();
            await turnContext.SendActivityAsync(turnContext.Activity, cancellationToken);
        }


private static Attachment CreateAdaptiveCardAttachment(string filePath, object data)
    {
        var adaptiveCardJson = File.ReadAllText(filePath);
        // Create a Template instance from the template payload
        AdaptiveCardTemplate template = new AdaptiveCardTemplate(adaptiveCardJson);

        string cardJson = template.Expand(data);

        var adaptiveCardAttachment = new Attachment()
        {
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = JsonConvert.DeserializeObject(cardJson),
        };
        return adaptiveCardAttachment;
    }

that can be an example:这可以是一个例子:

IEnumerable<AdaptiveCard> cards;    
await context.Context.SendActivityAsync((Activity)MessageFactory.Carousel(cards.Select(c => new Attachment
                    {
                        ContentType = AdaptiveCard.ContentType,
                        Content = c,
                    })));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM