我试图在Bot框架中的两个对话框之间传递IList<Attachment>
类型的对象。以下是我的代码现在的样子:
主对话框:
[Serializable]
public class CarouselCardsDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
//This object, 'attachments' is what I need to pass to the child dialog.
IList<Attachment> attachments = GetCardsAttachments();
context.Call(new CarouselDialog(attachments), ResumeAfterPrompt);
}
private async Task ResumeAfterPrompt(IDialogContext context, IAwaitable<string> result)
{
string input = await result;
await context.PostAsync(input);
context.Wait(MessageReceivedAsync);
}
private static IList<Attachment> GetCardsAttachments()
{
//Get a list of attachment
}
}
这是子对话框。我想将CarouselCardsDialog中的附件列表传递给此对象:
[Serializable]
public class CarouselDialog : IDialog<string>
{
private IList<Attachment> _attachments;
public CarouselDialog(IList<Attachment> attachments)
{
_attachments = attachments;
}
public async Task StartAsync(IDialogContext context)
{
IMessageActivity reply = context.MakeMessage();
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Attachments = _attachments;
await context.PostAsync(reply);
context.Wait(MessageReceivedAsync);
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
IMessageActivity activity = await result;
context.Done(activity.Text);
}
}
然而,当我运行此代码时,我收到此错误:
例外:输入&#39; Microsoft.Bot.Connector.Attachment&#39;在大会 &#39; Microsoft.Bot.Connector,Version = 3.5.1.0,Culture = neutral, 公钥= 31bf3856ad364e35&#39;未标记为可序列化。
我做错了什么?有办法解决这个问题吗?
答案 0 :(得分:4)
不确定异常,但解决方法是为您创建附件所需的所有数据创建模型,然后将此数据保存在上下文的UserData属性中。
context.UserData.SetValue<MyModel>("AttachmentsModel", instanceOfModel);
然后在第二个对话框中检索此模型
MyModel model;
context.UserData.TryGetValue<MyModel>("AttachmentsModel", out model);
我认为你也可以尝试在这个属性中放置一个IAttachment对象,但是模型会更好,因为它并不是真的用来容纳大对象。