我正在制作一些动态的"消息"它可以在运行时构建和链接在一起。
我有以下课程
[Serializable]
[JsonObject(IsReference = true)]
public class Node : INode
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual bool WaitForInput { get; set; }
public virtual int OnCompleteID { get; set; }
public virtual string OnCompleteName { get; set; }
public virtual Node OnCompleteNode { get; set; }
public virtual async Task OnExcecute(IDialogContext context, Activity activity)
{
if (OnCompleteNode != null)
if (WaitForInput)
context.Wait(OnCompleteNode.OnExcecuteAwait);
else
await OnCompleteNode.OnExcecute(context, activity);
}
public virtual async Task OnExcecuteAwait(IDialogContext context, IAwaitable<object> result)
{
Activity activity = await result as Activity;
await OnExcecute(context, activity);
}
}
以下类继承了哪些:
public class MessageNode : Node
{
/*public override int ID { get; set; }
public override string Name { get; set; }
public override bool WaitForInput { get; set; }
public override int OnCompleteID { get; set; }
public override string OnCompleteName { get; set; }
public override Node OnCompleteNode { get; set; }*/
public virtual string Message { get; set; }
public virtual List<Attachment> Attachments { get; set; }
public virtual string AttachmentLayoutType { get; set; }
public override async Task OnExcecute(IDialogContext context, Activity activity)
{
IMessageActivity reply = context.MakeMessage();
reply.Text = Message;
if (Attachments != null)
{
reply.Attachments = Attachments;
reply.AttachmentLayout = AttachmentLayoutType;
}
await context.PostAsync(reply);
await base.OnExcecute(context, activity);
}
}
最后我在RootDialog中构建数据
public Task StartAsync(IDialogContext context)
{
Schema.MessageNode preMN1 = new Schema.MessageNode();
preMN1.Message = "and then one more";
preMN1.ID = 2;
preMN1.WaitForInput = true;
Schema.MessageNode preMN = new Schema.MessageNode();
preMN.Message = "and then";
preMN.ID = 1;
preMN.OnCompleteNode = preMN1;
preMN.WaitForInput = true;
Schema.MessageNode messageNode = new Schema.MessageNode();
messageNode.Message = "test";
messageNode.ID = 0;
messageNode.OnCompleteNode = preMN;
messageNode.WaitForInput = true;
preMN1.OnCompleteNode = messageNode;
string jTest = JsonConvert.SerializeObject(messageNode);
context.Wait(messageNode.OnExcecuteAwait);
return Task.CompletedTask;
}
目前,首先调用messageNode的OnExcecuteAwait。哪个显示&#34;测试&#34;在聊天中,到目前为止一切都很好。因为WaitForInput标志设置为true,所以使用context.Wait调用链中的下一个节点(preMN)。当用户回复输入时,文本&#34;然后&#34;根据需要发送,而不是移动到链中的下一个节点。它停留在preMN并继续循环。在调试时,我发现OnCompleteNode属性(以及父节点类中定义的所有虚拟属性)都被清除。
奇怪的是,如果我覆盖MessageNode类中的虚拟属性,则context.Wait会完全中断,并且bot会不断返回RootDialog。 但是,如果我然后删除行&#34; preMN1.OnCompleteNode = messageNode;&#34;行为完美,僵尸程序按预期最终到达最后一个节点。我想这可能是由于在机器人状态被序列化时有一些循环阻止,但理想情况下我希望能够在需要时循环回到之前的消息。
答案 0 :(得分:1)
原来将[Serialized]属性添加到MessageNode类修复了该问题。谢谢你的提示Eric!