代码之家  ›  专栏  ›  技术社区  ›  Azmy

如何将按钮添加到HeroCard并在单击时选择值

  •  2
  • Azmy  · 技术社区  · 7 年前

    private async Task ShowSessionsHeroCard(IDialogContext context)
        {
            var replyToConversation = context.MakeMessage();
            replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            replyToConversation.Attachments = await GetSessionHeroCard(context);
            //replyToConversation.Attachments.Add(GetSessionHeroCard());
            await context.PostAsync(replyToConversation);
        }
    
        private async Task<List<Attachment>> GetSessionHeroCard(IDialogContext context)
        {
            List<Attachment> list = new List<Attachment>();
            List<CardAction> cardButtons = new List<CardAction>();
            foreach (var sessionDetails in scheduleList)
            {
                string[] session = GetSplittedDetails(sessionDetails);
    
                hospitalName = session[0]; //Hospital Name: {0}
                availableDay = session[1]; //Available Day: {1}
                appointmentNo = session[2]; // Appoinment No: {2}
                sessionAvailable = session[3]; // Session: {3}
    
                HeroCard hero = new HeroCard()
                {
                    Title = hospitalName,
                    Subtitle = availableDay,
                    Text = sessionAvailable + appointmentNo,
                    Buttons = cardButtons
                };
    
                list.Add(hero.ToAttachment());
    
            }
    
            CardAction getSessionValues = new CardAction()
            {
                Value = hospitalName + availableDay + sessionAvailable + appointmentNo,
                Type = ActionTypes.ImBack,
                Title = " Select Appointment "
            };
    
            cardButtons.Add(getSessionValues);
    
            string selectedAppointment = getSessionValues.Value.ToString();
            await GetSelectedAppointment(context, selectedAppointment);
    
            return list;
        }
    
         private async Task GetSelectedAppointment(IDialogContext context, string sessionSelected)
        {
            var replyToConversation = context.MakeMessage();
            //replyToConversation.AttachmentLayout = AttachmentLayoutTypes.List;
    
            string[] result = Utility.SplitSelectedApoitmentString(sessionSelected);
            var heroCard = new HeroCard()
            {
                Title = "Appointment Schedule",
                Subtitle = "These are the Appointment Details",
                Text = "Hospital selected : " + result[0] + "\n" + "Day of Appointment : " + result[1] + "\n" + "Time of Appointment : " + result[2] + "\n" + "Appointment Number" + result[3],
            }.ToAttachment();
    
            Attachment attachment = new Attachment()
            {
                Content = heroCard.Content,
                ContentType = heroCard.ContentType
            };
            replyToConversation.Attachments.Add(attachment);
            await context.PostAsync(replyToConversation);
    
        }
    

    1. 我是否正确定义了按钮?
    2. CardAction getSessionValues甚至在我单击之前就获得了一些值 在按钮上。如何避免这种情况。

    我想不出这里出了什么问题,请帮忙。(我肯定我做的事情很愚蠢。)

    提前感谢:)

    1 回复  |  直到 7 年前
        1
  •  2
  •   Ezequiel Jadib    7 年前

    GetSessionHeroCard 然而,我认为你对如何使用卡片有一个误解。

    1. 使用卡片操作创建卡片
    2. 将卡作为附件添加到机器人将发送给用户的回复消息中
    3. 在X方法中等待(可以在MessageReceivedAsync上,也可以在新方法中) context.Wait 。这是关键,因为此方法是在单击按钮后将接收具有值的消息的方法。

    您可以检查 RichCards 样本和 Contoso Flowers 示例,以便您可以看到它的实际效果。