我如何创建一个自适应卡,该卡将为漫游器答案创建一个表,但是该漫游器有许多行取决于从数据源获取的行,它将从数据源获取名称和ID,如何创建代码并在UI代码中将数据绑定到UI中的自适应卡上,在bot代码下面,我没有得到如何动态添加行和绑定数据。
public class EchoBot : ActivityHandler
{
private const string WelcomeText = @"This bot will introduce you to AdaptiveCards.
Type anything to see an AdaptiveCard.";
// This array contains the file location of our adaptive cards
private readonly string[] _cards =
{
//Path.Combine(".", "Resources", "FlightItineraryCard.json"),
//Path.Combine(".", "Resources", "ImageGalleryCard.json"),
//Path.Combine(".", "Resources", "LargeWeatherCard.json"),
//Path.Combine(".", "Resources", "RestaurantCard.json"),
// Path.Combine(".", "Resources", "SolitaireCard.json"),
Path.Combine(".", "Resources", "School.json"),
};
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
await SendWelcomeMessageAsync(turnContext, cancellationToken);
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
// Random r = new Random();
var fileRead = System.IO.File.ReadAllText("School.json");
var item = (JObject)JsonConvert.DeserializeObject(fileRead);
string classData = item["$data"].ToString();
AdaptiveTransformer transformer = new AdaptiveTransformer();
string cardJson = transformer.Transform(fileRead, classData);
Microsoft.Bot.Schema.Attachment attachment = new Microsoft.Bot.Schema.Attachment();
attachment.ContentType = "application/vnd.microsoft.card.adaptive";
attachment.Content = JsonConvert.DeserializeObject(cardJson);
var attachments = new List<Microsoft.Bot.Schema.Attachment>();
//var reply = MessageFactory.Attachment(attachments);
//reply.Attachments.Add(attachment);
//return reply;
// var cardAttachment = CreateAdaptiveCardAttachment(_cards[r.Next(_cards.Length)]);
//turnContext.Activity.Attachments = new List<Attachment>() { cardAttachment };
await turnContext.SendActivityAsync(MessageFactory.Attachment(attachments), cancellationToken);
await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);
}
private static async Task SendWelcomeMessageAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
foreach (var member in turnContext.Activity.MembersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(
$"Welcome to Adaptive Cards Bot {member.Name}. {WelcomeText}",
cancellationToken: cancellationToken);
}
}
}
private static Microsoft.Bot.Schema.Attachment CreateAdaptiveCardAttachment(string filePath)
{
var adaptiveCardJson = System.IO.File.ReadAllText(filePath);
var adaptiveCardAttachment = new Microsoft.Bot.Schema.Attachment()
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = JsonConvert.DeserializeObject(adaptiveCardJson),
};
return adaptiveCardAttachment;
}
答案 0 :(得分:0)
您无需将静态文件从路径中获取并放入附件中,而是需要将模板和数据分开。
检查以下内容:
https://docs.microsoft.com/en-us/adaptive-cards/templating/language
另外,这个例子中有一个如何分离模板和数据的例子,
How to map JSON Array with Adaptive Card Row - Using Designer to Create Template
这样做之后,您将需要使用模板API并使用代码来渲染卡。