Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向自适应卡动态添加行?_C#_Bots_Adaptive Cards - Fatal编程技术网

C# 如何向自适应卡动态添加行?

C# 如何向自适应卡动态添加行?,c#,bots,adaptive-cards,C#,Bots,Adaptive Cards,我如何创建一个自适应卡,它将为bot答案创建一个表,但bot有许多行取决于从数据源获取的行,它将从数据源获取名称和id,如何创建数据并在UI中为自适应卡绑定数据,如下bot代码:,我不知道如何动态添加行和绑定数据 public class EchoBot : ActivityHandler { private const string WelcomeText = @"This bot will introduce you to AdaptiveCards.

我如何创建一个自适应卡,它将为bot答案创建一个表,但bot有许多行取决于从数据源获取的行,它将从数据源获取名称和id,如何创建数据并在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;
    }
公共类EchoBot:ActivityHandler
{
private const string WelcomeText=@“此bot将向您介绍AdaptiveCards。
键入任何内容以查看AdaptiveCard。“;
//此阵列包含自适应卡的文件位置
专用只读字符串[]\u卡=
{
//Path.Combine(“.”,“Resources”,“flightinventurecard.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”),
};
MemberSaddedAsync上的受保护重写异步任务(IList membersAdded、iTurContext turnContext、CancellationToken CancellationToken)
{
等待SendWelcomeMessageAsync(turnContext,cancellationToken);
}
受保护的重写异步任务OnMessageActivityAsync(ITurnContext turnContext,CancellationToken CancellationToken)
{
//随机r=新随机();
var fileRead=System.IO.File.ReadAllText(“School.json”);
var item=(JObject)JsonConvert.DeserializeObject(fileRead);
string classData=item[“$data”].ToString();
自适应变压器=新的自适应变压器();
string cardJson=transformer.Transform(fileRead,classData);
Microsoft.Bot.Schema.Attachment Attachment=新的Microsoft.Bot.Schema.Attachment();
attachment.ContentType=“application/vnd.microsoft.card.adaptive”;
attachment.Content=JsonConvert.DeserializeObject(cardJson);
var attachments=新列表();
//var reply=MessageFactory.Attachment(附件);
//答复.附件.增加(附件);
//回复;
//var cardAttachment=CreateAdaptiveCardAttachment(_cards[r.Next(_cards.Length)]);
//turnContext.Activity.Attachments=新列表(){cardAttachment};
Wait turnContext.SendActivityAsync(MessageFactory.Attachment(attachments),cancellationToken);
Wait turnContext.SendActivityAsync(MessageFactory.Text(“请输入任何文本以查看另一张卡”)、cancellationToken);
}
专用静态异步任务SendWelcomeMessageAsync(ITurnContext turnContext,CancellationToken CancellationToken)
{
foreach(turnContext.Activity.MembersAdded中的变量成员)
{
if(member.Id!=turnContext.Activity.Recipient.Id)
{
等待turnContext.SendActivityAsync(
$“欢迎使用自适应卡Bot{member.Name}.{WelcomeText}”,
cancellationToken:cancellationToken);
}
}
}
私有静态Microsoft.Bot.Schema.Attachment CreateAptiveCardAttachment(字符串文件路径)
{
var adaptiveCardJson=System.IO.File.ReadAllText(文件路径);
var adaptiveCardAttachment=new Microsoft.Bot.Schema.Attachment()
{
ContentType=“application/vnd.microsoft.card.adaptive”,
Content=JsonConvert.DeserializeObject(adaptiveCardJson),
};
返回适配卡附件;
}

您需要分离模板和数据,而不是从path获取静态文件并将其放入附件中

检查以下各项:

另外,这个示例演示了如何将模板和数据分离


完成此操作后,您将需要使用模板API并使用代码呈现卡。

这是一个家庭作业问题吗?不,这不是一个家庭作业问题(因为此线程中有多人,如果希望我看到您的答复,您必须@提及我。)你能解释一下这些问题有什么不同吗?你还在研究这个问题吗?